def _getAndWriteToken(self): self.logger.debug('Starting to get access_token now...') self.sender.resetSession() isQueryOk = False for i in xrange(self.retry): try: accessTokenResponse = self.sender.get( 'https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=' + str(self.corpId) + '&corpsecret=' + str(self.secret)) isQueryOk = True break except: self.logger.warning('Getting access_token meets exception!') e = sys.exc_info()[0] self.logger.debug(str(e)) isQueryOk = False time.sleep(1) if not isQueryOk: return False self.logger.debug('The response code is:') self.logger.debug(str(accessTokenResponse)) myAccessTokenDict = accessTokenResponse.json() self.logger.debug('The response json is:') self.logger.debug(str(myAccessTokenDict)) if 'access_token' not in myAccessTokenDict: self.logger.info('Getting access_token fails!') if 'errcode' in myAccessTokenDict: self.logger.info('errcode is ' + str(myAccessTokenDict['errcode']) + '.') if 'errmsg' in myAccessTokenDict: self.logger.info('errmsg is ' + str(myAccessTokenDict['errmsg']) + '.') return False if 'expires_in' not in myAccessTokenDict: self.logger.info('expires_in is not in the response json!') return False self.accessTokenDict = myAccessTokenDict self.accessTokenDict['expiresTime'] = self.getExpireTime(self.accessTokenDict['expires_in']) if not NxFiles.isDir(self.accessTokenStoragePath): NxFiles.makeDirs(self.accessTokenStoragePath) myFile = open(self.accessTokenFile, 'w') myFile.write(str(self.accessTokenDict['expiresTime']) + '\n') myFile.write(str(self.accessTokenDict['access_token']) + '\n') myFile.write(str(self.accessTokenDict['expires_in']) + '\n') myFile.close() return True
def httpDownload(url, cookieString, location, isEntireConfig=True, fileName=None, timeout=60, logger=None): if logger is None: logger = logging.getLogger() if fileName is None: if isEntireConfig: fileName = 'AdcConfig' + time.strftime('%Y%m%d%H%M%S', time.localtime()) + '.tar' else: fileName = 'AdcConfig' + time.strftime('%Y%m%d%H%M%S', time.localtime()) logger.debug('Checking the location ' + str(location)) if not NxFiles.isDir(location): logger.debug('The location ' + str(location) + ' is not a directory, attempting to make it.') NxFiles.makeDirs(location) logger.debug('The location ' + str(location) + ' has been made.') else: logger.debug('The location ' + str(location) + ' is a valid directory.') if NxFiles.isFile(location + '/' + fileName): logger.warning('The file ' + location + '/' + fileName + ' exists, delete it now!') NxFiles.removeForce(location + '/' + fileName) try: r = NxRequests() r.addCookie('adc_session', cookieString) r.setTimeout(timeout) r.addHeader( 'Accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8' ) r.addHeader('Accept-Encoding', 'gzip, deflate, sdch') r.addHeader('Accept-Language', 'en-US,en;q=0.8') r.addHeader('Connection', 'keep-alive') r.addHeader('Upgrade-Insecure-Requests', '1') r.addHeader( 'User-Agent', 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.116 Safari/537.36' ) #r.addHeader('referer', refererString) logger.info('Starting to download...') response = r.get(url, stream=True) if response.status_code != 200: logger.warning( 'The status code from ADC is not 200, return False!') #wechat2Me('[AdcPatroller] The status code from ADC is not 200! URL is '+str(url)+' .') return False if not response.content: logger.warning( 'The response from ADC has no content, return False!') #wechat2Me('[AdcPatroller] The response from ADC has no content! The URL is '+str(url)+' .') return False else: if str(response.content).find(invalidLicenseString) != -1: logger.warning('The response content contains ' + invalidLicenseString + ' , return False!') return False with open(location + '/' + fileName, 'wb') as f: for chunk in response.iter_content(chunk_size=1024): if chunk: f.write(chunk) logger.info('The config file has been downloaded to ' + location + '/' + fileName + ' .') except Exception as err: logger.debug('Downloading meete exception:') logger.debug(str(err)) return False
def forceMakeDirectory(path, logger=None): if logger is None: logger = logging.getLogger() if not NxFiles.isDir(path): logger.info(str(path) + ' does not exist, attempting to make it.') NxFiles.makeDirs(path)