def get_records(ta_id): ta = tasver.WebOpRecordReq() ta.investorid = ta_id header = tasver.Header() header.cmd = 103 header.ver = 0 header.platform = 3 header.content = pbjson.pb2json(ta) headdata = pbjson.pb2json(header) headers = {'content-type': 'application/json'} r = requests.post(url,headdata,headers=headers) headerRes = tasver.WebRspHeader() headerRes.ParseFromString(r.content) if headerRes.errCode == 0 : oAccountRsp = tasver.WebOpRecordRsp() oAccountRsp.ParseFromString(headerRes.content) oAccountJon = pbjson.pb2json(oAccountRsp) else : print "ErrCode:" print headerRes.errCode print "ErrMsg:" print headerRes.errMsg.encode("utf-8") records = json.loads(oAccountJon) funds = [] for item in records['op']: if {'fundname':item['fundname'],'fundid':item['fundid']} not in funds: fund = {'fundname':item['fundname'],'fundid':item['fundid']} funds.append(fund) content = {'funds':funds,'fundsCount':len(funds),'recordsList':records['op'],'len':len(records['op'])} return content
def fund_info(request): ta = tasver.WebFundDetailReq() ta.fundid = request.GET['fundid'] print ta.fundid header = tasver.Header() header.cmd = 107 header.ver = 0 header.platform = 3 header.content = pbjson.pb2json(ta) headdata = pbjson.pb2json(header) headers = {'content-type': 'application/json'} r = requests.post(url,headdata,headers=headers) headerRes = tasver.WebRspHeader() headerRes.ParseFromString(r.content) if headerRes.errCode == 0 : print headerRes.errCode oAccountRsp = tasver.WebFundDetailRsp() oAccountRsp.ParseFromString(headerRes.content) oAccountJon = pbjson.pb2json(oAccountRsp) content = {'fundinfo':json.loads(oAccountJon)} return JsonResponse(content) else : print "ErrCode:" print headerRes.errCode print "ErrMsg:" print headerRes.errMsg.encode("utf-8")
def accountHomeRse(taid,time): ta = tasver.AccountHomeReq() ta.investorid = taid ta.reqday = time header = tasver.Header() header.cmd = 104 header.ver = 0 header.platform = 3 header.content = pbjson.pb2json(ta) headdata = pbjson.pb2json(header) headers = {'content-type': 'application/json'} r = requests.post(url,headdata,headers=headers) headerRes = tasver.WebRspHeader() headerRes.ParseFromString(r.content) if headerRes.errCode == 0 : oAccountRsp = tasver.AccountHomeRsp() oAccountRsp.ParseFromString(headerRes.content) oAccountJon = pbjson.pb2json(oAccountRsp) print json.loads(oAccountJon) return json.loads(oAccountJon) else : print "ErrCode:" print headerRes.errCode print "ErrMsg:" print headerRes.errMsg.encode("utf-8")
def get_all_product(): ta = tasver.ProductListReq() header = tasver.Header() header.cmd = 102 header.ver = 0 header.platform = 3 header.content = pbjson.pb2json(ta) headdata = pbjson.pb2json(header) headers = {'content-type': 'application/json'} print '!!!!!!!begin',time.time() r = requests.post(url,headdata,headers=headers) print '!!!!!!!end',time.time() headerRes = tasver.WebRspHeader() print '____________step1',time.time() headerRes.ParseFromString(r.content) print '____________step2',time.time() if headerRes.errCode == 0 : oAccountRsp = tasver.ProductListRsp() print '____________step3',time.time() oAccountRsp.ParseFromString(headerRes.content) print '____________step4',time.time() oAccountJon = pbjson.pb2json(oAccountRsp) print '____________step5',time.time() else : print "ErrCode:" print headerRes.errCode print "ErrMsg:" print headerRes.errMsg.encode("utf-8") print '____________step6',time.time() alllist = json.loads(oAccountJon)['productlist'] print alllist print '____________step7',time.time() return alllist
def s2cdata(self, data): """ 处理返回的二进制数据 :param data: 二进制数据 :param s2cname: :return: """ # 实例化rpc s2crpc = rpc_pb2.s2cenvelope() # 反序列化rpc s2crpc.ParseFromString(data) # 返回的协议id protoid = s2crpc.service_no # 返回的协议数据 protodata = s2crpc.payload # 通过id反查协议名 for each in rpc_pb2._SERVICENO.values: if each.number == protoid: s2cname = each.name break else: raise BaseException, u"没有找到对应的协议名" # 打印协议名 print u"返回数据的协议名为:" print s2cname # 实例化对应的协议 s2cproto = eval(s2cname)() # 反序列化协议 s2cproto.ParseFromString(protodata) # 用json格式打印协议 print u"返回数据的json格式为:" print pbjson.pb2json(s2cproto) # 提取协议中需要的数据 s2clist = getattr(S2C_PROTO(), s2cname)(s2cproto) return s2clist
def webFundAssetReq(taid): ta = tasver.WebFundAssetReq() ta.fundid = "S33180" ta.investorid = "DI1042" header = tasver.Header() header.cmd = 106 header.ver = 0 header.platform = 3 header.content = pbjson.pb2json(ta) headdata = pbjson.pb2json(header) headers = {'content-type': 'application/json'} r = requests.post(url,headdata,headers=headers) headerRes = tasver.WebRspHeader() headerRes.ParseFromString(r.content) if headerRes.errCode == 0 : oAccountRsp = tasver.WebFundAssetRsp() oAccountRsp.ParseFromString(headerRes.content) oAccountJon = pbjson.pb2json(oAccountRsp) return json.loads(oAccountJon) else : print "ErrCode:" print headerRes.errCode print "ErrMsg:" print headerRes.errMsg.encode("utf-8")
def get_TAID(idtype, idno, name, moblie): ta = tasver.WebVerifyIdReq() ta.idtype = int(idtype) ta.idno = idno ta.name = unicode(name, "utf-8") ta.phone = moblie header = tasver.Header() header.cmd = 101 header.ver = 0 header.platform = 3 header.content = pbjson.pb2json(ta) headdata = pbjson.pb2json(header) headers = {'content-type': 'application/json'} r = requests.post(url,headdata,headers=headers) headerRes = tasver.WebRspHeader() headerRes.ParseFromString(r.content) if headerRes.errCode == 0 : oAccountRsp = tasver.WebVerifyIdRsp() oAccountRsp.ParseFromString(headerRes.content) oAccountJon = pbjson.pb2json(oAccountRsp) return json.loads(oAccountJon)['id'] else : print "ErrCode:" print headerRes.errCode print "ErrMsg:" print headerRes.errMsg.encode("utf-8") return "_"
def report_create(request): ta = tasver.FundReportReq() ta.reportid = int(request.GET['reportid']) header = tasver.Header() header.cmd = 108 header.ver = 0 header.platform = 3 header.content = pbjson.pb2json(ta) headdata = pbjson.pb2json(header) headers = {'content-type': 'application/json'} r = requests.post(url, headdata, headers=headers) headerRes = tasver.WebRspHeader() headerRes.ParseFromString(r.content) if headerRes.errCode == 0: oAccountRsp = tasver.FundReportRsp() oAccountRsp.ParseFromString(headerRes.content) BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) REPORT_ROOT = os.path.join(BASE_DIR, 'gaoyicrm/static/reports') WriteFileData = open( REPORT_ROOT + '/' + (oAccountRsp.reportname).encode('utf-8'), 'wb') WriteFileData.write(oAccountRsp.content) WriteFileData.close() # 修复基金报告导出问题 # print oAccountRsp return oAccountRsp else: print "ErrCode:" print headerRes.errCode print "ErrMsg:" print headerRes.errMsg.encode("utf-8")
def start_grpc(): channel = implementations.insecure_channel(IP, PortGrpc) stub = ranker_pb2.beta_create_SimilarityService_stub(channel) grpc_req = ranker_pb2.FeatureRankingRequest() grpc_req.ReqId = 1 grpc_req.Type = 1 compare_image_file = open(CompareImage, "rb") compare_image_content = base64.b64encode(compare_image_file.read()) compare_image_file.close() grpc_req.Image.Id = "test" grpc_req.Image.BinData = compare_image_content v_json_file = open(CandidatesDataFile) json_data = json.load(v_json_file) id = 1 start = 0 for v in json_data["Vehicles"]: if start > CandidatesStartIndex: feature = v["Features"] if feature == "": continue candidate = grpc_req.Candidates.add() candidate.Id = id candidate.Feature = feature id = id + 1 start = start + 1 if id >= CandidatesCount: break # append the best candidate to the tail best_candidate = grpc_req.Candidates.add() best_candidate.Id = id best_candidate.Feature = CompareImageFeature # write the request data into file in case debug req_file = open(ReqDataFile, 'w') post_json_data = pbjson.pb2json(grpc_req) json.dump(post_json_data, req_file) req_file.close() print "Save the request data into file: %s " % (ReqDataFile) # call ranker service start_time = time.time() resp = stub.GetRankedVector(grpc_req, 100000 * 100) end_time = time.time() print "Ranker cost: %d ms with %d candidates" % ((end_time - start_time) * 1000, id - 1) resp_json = pbjson.pb2json(resp) # write ranker results resp_file = open(RespDataFile, 'w') resp_file.write(resp_json) resp_file.close() print "Save the response date into file: %s " % (RespDataFile)
def weixininfo(request): result = requests.get(request.GET['weixinUrl']) resultJson = result.json() openid = resultJson['openid'] print openid userInfoUrl = 'https://api.weixin.qq.com/sns/userinfo?access_token=' + resultJson[ 'access_token'] + '&openid=' + resultJson['openid'] userInfoResult = requests.get(userInfoUrl) userInfoResultJson = userInfoResult.json() content = { 'usernickname': userInfoResultJson['nickname'], 'headimgurl': userInfoResultJson['headimgurl'] } if Investor.objects.filter(openid=openid).exists(): investor = Investor.objects.get(openid=openid) request.session['userID'] = investor.id content.update({'success': 0}) else: ta = tasver.WechatIdCheckReq() ta.wechatid = resultJson['openid'] # ta.wechatid = 'o3zFCuLQfGhhwdaenalaIr8R9aQs' header = tasver.Header() header.cmd = 109 header.ver = 0 header.platform = 3 header.content = pbjson.pb2json(ta) headdata = pbjson.pb2json(header) headers = {'content-type': 'application/json'} r = requests.post(url, headdata, headers=headers) headerRes = tasver.WebRspHeader() headerRes.ParseFromString(r.content) if headerRes.errCode == 0: oAccountRsp = tasver.WechatIdCheckRsp() oAccountRsp.ParseFromString(headerRes.content) oAccountJon = pbjson.pb2json(oAccountRsp) if json.loads(oAccountJon)['investorid'] == '': request.session['openid'] = openid else: request.session['openid'] = json.loads( oAccountJon)['investorid'] content.update({'success': 1}) # print json.loads(oAccountJon) else: print "ErrCode:" print headerRes.errCode print "ErrMsg:" # print headerRes.errMsg.encode("utf-8") return JsonResponse(content)
def fund_details_chart(request): page = request.GET['page'] ta = tasver.WebFundDetailReq() ta.fundid = request.GET['fundid'] header = tasver.Header() header.cmd = 107 header.ver = 0 header.platform = 3 header.content = pbjson.pb2json(ta) headdata = pbjson.pb2json(header) headers = {'content-type': 'application/json'} r = requests.post(url,headdata,headers=headers) headerRes = tasver.WebRspHeader() headerRes.ParseFromString(r.content) if headerRes.errCode == 0 : oAccountRsp = tasver.WebFundDetailRsp() oAccountRsp.ParseFromString(headerRes.content) oAccountJon = pbjson.pb2json(oAccountRsp) detailInfo = json.loads(oAccountJon) nav_paginator = Paginator(detailInfo['fundNavlist'],10) # print detailInfo['fundNavlist'] # totalnav_paginator = Paginator(detailInfo['totalnav'],10) # print totalnav_paginator sh300_paginator = Paginator(detailInfo['sh300'],10) gem_paginator = Paginator(detailInfo['gem'],10) subNavPaginatorList = [] totalNavPaginatorList = [] subsh300PaginatorList = [] subgemPaginatorList = [] for item in nav_paginator.page(int(page)): subNavPaginatorList.append(item) # for item in totalnav_paginator.page(int(page)): # totalNavPaginatorList.append(item) for item in sh300_paginator.page(int(page)): subsh300PaginatorList.append(item) for item in gem_paginator.page(int(page)): subgemPaginatorList.append(item) paginatorlist = [subNavPaginatorList,subsh300PaginatorList,subgemPaginatorList] print paginatorlist content = {'detailInfo':detailInfo,'paginatorList':paginatorlist,'len':len(nav_paginator.page(int(page))),'pageCount':nav_paginator.num_pages} return JsonResponse(content) else : print "ErrCode:" print headerRes.errCode print "ErrMsg:" print headerRes.errMsg.encode("utf-8")
def read_pb(input_filename, output_filename): data = EL() with open(input_filename, 'rb') as f: data.ParseFromString(f.read()) f.close() output_basename, output_extension = os.path.splitext(output_filename) with open(output_filename, 'w') as out: if output_extension == ".json": out.write(pb2json(data)) out.close() elif output_extension == ".xml": out.write("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n"); dumpXML(out, data) out.write("\n") out.close() elif output_extension == ".cpp" or output_extension == ".cc" or output_extension == ".java": with open(output_filename + ".xml", 'w') as xml_out: dumpXML(xml_out, data) xml_out.close() p = subprocess.Popen(["srcml", output_filename + ".xml"], stdout=subprocess.PIPE) for line in p.stdout: out.write(line) out.close() os.remove(output_filename + ".xml") else: # do not output, to test performance of loading protobuf out.close()
def read_pb(input_filename, output_filename): data = review.Data() with open(input_filename, 'rb') as f: data.ParseFromString(f.read()) f.close() with open(output_filename, 'w') as out: out.write(pb2json(data)) out.close()
def write_to_jsonfile(filename): address_book = addressbook_pb2.AddressBook() person = address_book.persons.add() person.age = 120 person.name = "Safly" print(" 1).person -> \n", person, type(person)) #Convert Protobuf to json json_data = pbjson.pb2json(address_book) print(" 2).convert protobuf to json -> \n", json_data) print(" 3).write to file ...") with open(filename, 'w') as file_obj: file_obj.write(str(json_data))
def s2cdata(self, data): """ 处理接收到的二进制数据 :param data: 二进制数据 :param s2cname: :return: """ s2clist = [] # 实例化rpc s2crpc = rpc_pb2.s2cenvelope() # 反序列化rpc s2crpc.ParseFromString(data) # 返回的协议id protoid = s2crpc.service_no # 返回的协议数据 protodata = s2crpc.payload # 通过id反查协议名 for each in rpc_pb2._SERVICENO.values: if each.number == protoid: s2cname = each.name break else: raise BaseException, u"cannot find s2c proto name, maybe new add" # 打印协议名 print u"返回数据的协议名为:", s2cname # 实例化对应的协议 s2cproto = eval(s2cname)() # 反序列化协议 s2cproto.ParseFromString(protodata) # 用json格式打印协议 protojson = pbjson.pb2json(s2cproto) print u"返回数据的json格式为:" print protojson # 提取协议中需要的数据 try: s2clist = getattr(self.s2c_proto_class, s2cname)(s2cproto) except AttributeError as e: print e print u"没有需要提取的数据" return s2cname, protojson, s2clist
{ "number": "+1234567890", "type": 0 }, { "number": "+2345678901", "type": 1 } ] }, { "name": "Ben Bun", "id": 2, "email": "*****@*****.**", "phone": [ { "number": "+1234567890", "type": 0 } ] } ] } #Convert JSoN to Protobuf adr_book = pbjson.dict2pb(AddressBook, adr_book_json) #Convert Protobuf to JSoN new_json = pbjson.pb2json(adr_book)
adr_book_json = { "person": [{ "name": "Mohamed Lee", "id": 1, "phone": [{ "number": "+1234567890", "type": 0 }, { "number": "+2345678901", "type": 1 }] }, { "name": "Ben Bun", "id": 2, "email": "*****@*****.**", "phone": [{ "number": "+1234567890", "type": 0 }] }] } #Convert JSoN to Protobuf adr_book = pbjson.dict2pb(AddressBook, adr_book_json) #Convert Protobuf to JSoN new_json = pbjson.pb2json(adr_book)
## REQUIRES simplejson, protobuf, and a copy of pbjson.py from https://github.com/NextTuesday/py-pb-converters # path... src/github.com/dlamblin/mta-delay-monitoring/build/generated/source/proto/main/python/nyct_subway_pb2.py import os base_dir = os.path.dirname(__file__) or '.' import sys sys.path.insert(0, os.path.join(base_dir, 'build', 'generated', 'source', 'proto', 'main', 'python')) # import build.generated.source.proto.main.python.gtfs_realtime_pb2 as gtfs__realtime__pb2 # from build.generated.source.proto.main.python.nyct_subway_pb2 import TripReplacementPeriod import build.generated.source.proto.main.python.gtfs_realtime_pb2 as gtfs_realtime_pb2 import build.generated.source.proto.main.python.nyct_subway_pb2 as nyct_subway_pb2 import pbjson import simplejson file_to_open = sys.argv[1] or 'gtfs' gtfsFeedMessage = gtfs_realtime_pb2.FeedMessage try: f = open(file_to_open, "rb") # read binary b = f.read() print("\n\tBINARY GTFS: " + b.hex()[:20] + "... " + str(len(b)) + " bytes.\n") gtfsFeedMessage.ParseFromString(self=gtfsFeedMessage, serialized=b) f.close() except IOError: print (file_to_open + ": Could not open file. Creating a new one.") print('Okay the Protocol Buffer that was generated claims to have a syntax error so...') print(pbjson.pb2json(gtfsFeedMessage))
def matchData(self, is_upload_url, config_data, url, params, response, api_json_data): isSuccess = False aa = {'params': {'success': ''}} c_url = config_data['url'] # 配置文件的 url c_request = [] c_response = [] if 'request' in config_data: c_request = config_data['request'] # 配置文件的 请求参数 if 'response' in config_data: c_response = config_data['response'] # 配置文件的 响应参数 if len(response) > 0: response = eval( response.replace('true', 'True').replace('false', 'False')) if self.typeof(response) == 'str': response = eval(response) # 如果当前为 上报 url 则 进行 protobuf 解码 后再解析 if is_upload_url: # 获取 当前上报类型 urlList = url.split('/') name = urlList[len(urlList) - 1] # 判断 当前 上报 url 类型 是否存在 protobuf 文件里面 if name in self.logTypeList[self.sdk_type]: b64_params = base64.b64decode(params) # 先 base64 decode 原上报数据 receiveData = self.logTypeList[self.sdk_type][ name] # 获取 对应的 上报类型对象 receiveData.ParseFromString( b64_params) # protobuf 对象转 string 类型 params = pbjson.pb2json(receiveData) # protobuf 数据转 json 对象 params = eval(params.replace( 'true', 'True')) # python 不支持 小写 bool 类型 , 所以需要 替换成 大写 else: print('当前上报类型 不存在 protobuf 里面 , type : ' + name) return isSuccess print('\n准备分析 url : ' + url) self.code = api_json_data['code'] self.mode = 'request' requestList = self.startMatch(c_request, params) self.mode = 'response' responseList = self.startMatch(c_response, response) self.mode = '' print('1 请求成功的 Key : ' + str(requestList[0])) if not (len(sys.argv) >= 2 and sys.argv[1] in 'input'): print('1 响应成功的 Key : ' + str(responseList[0]) + '\n') print('2 请求失败的 Key : ' + str(requestList[1])) if not (len(sys.argv) >= 2 and sys.argv[1] in 'input'): print('2 响应失败的 Key : ' + str(responseList[1]) + '\n') print('3 请求警告的 Key : ' + str(requestList[2])) if not (len(sys.argv) >= 2 and sys.argv[1] in 'input'): print('3 响应警告的 Key : ' + str(responseList[2]) + '\n') print('4 请求错误的 Key : ' + str(requestList[3])) if not (len(sys.argv) >= 2 and sys.argv[1] in 'input'): print('4 响应错误的 Key : ' + str(responseList[3])) # 保存分析记录 self.saveData(api_json_data['code'], api_json_data['method'], url, params, response, requestList, responseList) if len(requestList[1]) <= 0 and len(responseList[1]) <= 0 and len( requestList[3]) <= 0 and len(responseList[3]) <= 0: isSuccess = True return isSuccess