def saveVideoDataToHttp(userId, gameRound): # 调用http videoId = '%s_%s' % (userId, gameRound.roundId) url = '%s/dizhu/tupt/replay/video/save?videoId=%s' % (gdata.httpGame(), videoId) gameRoundDict = GameRoundCodecDictReplay().encode(gameRound) gameRoundDict['uid'] = userId if ftlog.is_debug(): ftlog.debug('obsystem.saveVideoDataToHttp userId=', userId, 'videoId=', videoId, 'url=', url, 'gameRoundDict=', gameRoundDict) response = None try: _, response = http.runHttp('POST', url, None, strutil.dumps(gameRoundDict), 3, 6) datas = strutil.loads(response) ec = datas.get('ec', 0) if ec == 0: return videoId raise TYBizException(ec, datas.get('info', '')) except TYBizException: raise except: ftlog.error(url, 'response=', response) raise TYBizException(-1, '保存失败')
def uploadVideo(uploadUrl, token, uploadPath, videoData): filename = os.path.basename(uploadPath) formItems = [] formItems.append(FormItemData('token', token)) formItems.append(FormItemData('key', uploadPath)) formItems.append(FormItemFile('file', videoData, filename)) # formItems.append(FormItemFile('fileBinaryData', videoData, filename)) boundary = genBoundary() uploadData = encodeFormItems(boundary, formItems) headers = { 'Content-Type': ['multipart/form-data; charset=utf-8; boundary=%s' % (boundary)] } try: code, body = http.runHttp(method='POST', url=uploadUrl, header=headers, body=uploadData, connect_timeout=5, timeout=5) if ftlog.is_debug(): ftlog.debug('uploader.uploadVideo uploadUrl=', uploadUrl, 'uploadPath=', uploadPath, 'token=', token, 'ret=', (code, body)) if code == 200: return 0, body ftlog.info('uploader.uploadVideo Res uploadUrl=', uploadUrl, 'uploadPath=', uploadPath, 'token=', token, 'ret=', code) return -1, '上传失败' except: return -2, '上传失败'
def uploadVideo(uploadUrl, token, uploadPath, videoData): filename = os.path.basename(uploadPath) formItems = [] formItems.append(FormItemData('token', token)) formItems.append(FormItemData('key', uploadPath)) formItems.append(FormItemFile('file', videoData, filename)) boundary = genBoundary() uploadData = encodeFormItems(boundary, formItems) headers = { 'Content-Type': ['multipart/form-data; charset=utf-8; boundary=%s' % (boundary)] } try: code, body = http.runHttp(method='POST', url=uploadUrl, header=headers, body=uploadData, connect_timeout=5, timeout=5) if ftlog.is_debug(): ftlog.debug('uploader.uploadVideo uploadUrl=', uploadUrl, 'uploadPath=', uploadPath, 'token=', token, 'ret=', (code, body)) if code == 200: return 0, uploadPath ftlog.info('uploader.uploadVideo Fail uploadUrl=', uploadUrl, 'uploadPath=', uploadPath, 'token=', token, 'ret=', (code, body)) return -1, '上传失败' except: return -2, '上传失败'
def testSendBi(url, header, data): from freetime.aio import http _start = time.time() _retry = 4 _timeout = [0.1, 0.3, 0.5, 0.7] for x in xrange(_retry): code, _ = http.runHttp('POST', url, header, data, _timeout[x], _timeout[x]) if code == 200: return (True, x, time.time() - _start) return (False, x, 0)
def doHttp(method, url, header={}, body="", conntimeout=2, timeout=2): return fthttp.runHttp(method, url, header, body, conntimeout, timeout)
def webget(httpurl, querys={}, appKey=None, postdata_='', method_='POST', headers_={}, cookies={}, connect_timeout=3, timeout=3, needresponse=True, codeKey='code', appKeyTail=None, filterParams=[], connector="&"): ''' 调用远程HTTP接口, 并返回JSON结果 ''' params = [] if isinstance(querys, (list, tuple)): params.extend(querys) elif isinstance(querys, dict): keys = querys.keys() keys.sort() for k in keys: params.append(k) params.append(querys[k]) for x in xrange(len(params)): param = params[x] if isinstance(param, unicode): param = param.encode('utf8') else: param = str(param) params[x] = param query2 = [] query = [] for x in xrange(len(params) / 2): k = params[x * 2] v = params[x * 2 + 1] if k not in filterParams: query.append(k + '=' + v) if k == 'authInfo': # TODO 这部分代码是最老版的第三方的支持 query2.append(k + '=' + v) else: query2.append(k + '=' + urllib.quote(v)) query = connector.join(query) query2 = '&'.join(query2) if appKey: if appKeyTail: md5str = str(appKey) + query + str(appKeyTail) else: md5str = str(appKey) + query + str(appKey) ftlog.debug("webgetGdss,md5str:", md5str) md5code = strutil.md5digest(md5str) query2 = query2 + '&' + codeKey + '=' + md5code if isinstance(httpurl, unicode): httpurl = httpurl.encode('utf8') if len(query2) > 0: if httpurl.find('?') > 0: httpurl = httpurl + '&' + query2 else: httpurl = httpurl + '?' + query2 if isinstance(httpurl, unicode): httpurl = httpurl.encode('utf8') if postdata_ and isinstance(postdata_, dict): postdata_ = urllib.urlencode(postdata_) if headers_ == None or len(headers_) == 0: headers_ = {'Content-type': ['application/x-www-form-urlencoded']} if needresponse: _, hbody = http.runHttp(method=method_, url=httpurl, header=headers_, body=postdata_, connect_timeout=connect_timeout, timeout=timeout) return hbody, httpurl else: http.runHttpNoResponse(method_, httpurl, headers_, postdata_, connect_timeout) return None, httpurl
def doHttp(method, url, header={}, body="", conntimeout=2, timeout=2): return fthttp.runHttp(method, url, header, body, conntimeout, timeout)
def webget(httpurl, querys={}, appKey=None, postdata_='', method_='POST', headers_={}, cookies={}, connect_timeout=3, timeout=3, needresponse=True, codeKey='code', appKeyTail=None, filterParams=[], connector="&"): ''' 调用远程HTTP接口, 并返回JSON结果 ''' params = [] if isinstance(querys, (list, tuple)): params.extend(querys) elif isinstance(querys, dict): keys = querys.keys() keys.sort() for k in keys: params.append(k) params.append(querys[k]) for x in xrange(len(params)): param = params[x] if isinstance(param, unicode): param = param.encode('utf8') else: param = str(param) params[x] = param query2 = [] query = [] for x in xrange(len(params) / 2): k = params[x * 2] v = params[x * 2 + 1] if k not in filterParams: query.append(k + '=' + v) if k == 'authInfo': # TODO 这部分代码是最老版的第三方的支持 query2.append(k + '=' + v) else: query2.append(k + '=' + urllib.quote(v)) query = connector.join(query) query2 = '&'.join(query2) if appKey: if appKeyTail: md5str = str(appKey) + query + str(appKeyTail) else: md5str = str(appKey) + query + str(appKey) ftlog.debug("webgetGdss,md5str:", md5str) md5code = strutil.md5digest(md5str) query2 = query2 + '&' + codeKey + '=' + md5code if isinstance(httpurl, unicode): httpurl = httpurl.encode('utf8') if len(query2) > 0: if httpurl.find('?') > 0: httpurl = httpurl + '&' + query2 else: httpurl = httpurl + '?' + query2 if isinstance(httpurl, unicode): httpurl = httpurl.encode('utf8') if postdata_ and isinstance(postdata_, dict): postdata_ = urllib.urlencode(postdata_) if headers_ == None or len(headers_) == 0: headers_ = {'Content-type': ['application/x-www-form-urlencoded']} if needresponse: _, hbody = http.runHttp(method=method_, url=httpurl, header=headers_, body=postdata_, connect_timeout=connect_timeout, timeout=timeout) return hbody, httpurl else: http.runHttpNoResponse(method_, httpurl, headers_, postdata_, connect_timeout) return None, httpurl