def test_escaped_unicode_string(self): unicode_json = b'"hello \\u0e01\\u0e02\\u0e03\\ud835\\udcab\\udb40\\udc70 unicode"' unicode_text = u'hello \u0e01\u0e02\u0e03\U0001D4AB\U000E0070 unicode' buf = TTransport.TMemoryBuffer(unicode_json) transport = TTransport.TBufferedTransportFactory().getTransport(buf) protocol = TJSONProtocol(transport) if sys.version_info[0] == 2: unicode_text = unicode_text.encode('utf8') self.assertEqual(protocol.readString(), unicode_text)
def create( cls, host, port=DEFAULT_AURORA_PORT, encoding='json'): """ Parses a host name and sets up Torando and Thrift client """ # To recieve and send binary thrift to the Aurora master we need to set # headers appropriately. transport = THttpClient('http://%s:%s/api' % (host, str(port))) # Set aurora credentials in transport header if the environment # variables have been set if os.getenv('AURORA_USERNAME') and os.getenv('AURORA_PASSWORD'): username = os.getenv('AURORA_USERNAME') password = os.getenv('AURORA_PASSWORD') credentials = base64.encodestring( '%s:%s' % (username, password)).replace('\n', '') auth_header = "Basic %s" % credentials transport.setCustomHeaders({'Authorization': auth_header}) if encoding == 'binary': transport.setCustomHeaders({ 'Content-Type': 'application/vnd.apache.thrift.binary', 'Accept': 'application/vnd.apache.thrift.binary', }) protocol = TBinaryProtocol(transport) elif encoding == 'json': protocol = TJSONProtocol(transport) else: raise Exception('Unknown encoding %s' % encoding) client = Client(protocol) return client
def post(self, request): out_format = request.POST.get('OutFormat', 'json') info = CouponInfo( cid=coupon['cid'], name=coupon['name'], amount=coupon['amount'], progress=coupon['progress'], start_time=coupon['start_time'], expired_time=coupon['expired_time'], ctype=coupon['ctype'], use_type=coupon['use_type'], min_invest_time=coupon['min_invest_time'], max_invest_time=coupon['max_invest_time'], min_amount=coupon['min_amount'], des=coupon['desc'], ) res = CouponInfoResponse(code=0, message='success', info=info) tMemory_b = TMemoryBuffer() if out_format == 'binary': tBinaryProtocol_b = TCompactProtocol(tMemory_b) content_type = 'application/octet-stream' elif out_format == 'tjson': tBinaryProtocol_b = TJSONProtocol(tMemory_b) content_type = 'application/json' else: tBinaryProtocol_b = TSimpleJSONProtocol(tMemory_b) content_type = 'application/json' res.write(tBinaryProtocol_b) memory_buffer = tMemory_b.getvalue() return HttpResponse(content=memory_buffer, content_type=content_type)
def create(cls, host, port=DEFAULT_AURORA_PORT, encoding="json"): """ Parses a host name and sets up Torando and Thrift client """ # To recieve and send binary thrift to the Aurora master we need to set # headers appropriately. transport = THttpClient("http://%s:%s/api" % (host, str(port))) # Set aurora credentials in transport header if the environment # variables have been set if os.getenv("AURORA_USERNAME") and os.getenv("AURORA_PASSWORD"): username = os.getenv("AURORA_USERNAME") password = os.getenv("AURORA_PASSWORD") credentials = base64.encodestring( "%s:%s" % (username, password) ).replace("\n", "") auth_header = "Basic %s" % credentials transport.setCustomHeaders({"Authorization": auth_header}) if encoding == "binary": transport.setCustomHeaders( { "Content-Type": "application/vnd.apache.thrift.binary", "Accept": "application/vnd.apache.thrift.binary", } ) protocol = TBinaryProtocol(transport) elif encoding == "json": protocol = TJSONProtocol(transport) else: raise Exception("Unknown encoding %s" % encoding) client = Client(protocol) return client
def done(self): self.stop() context = profilingContext(self.context.roots) # print(context.methods) with open("python-profiler-result.jandy", "w") as f: tp = TFileObjectTransport(f) context.write(TJSONProtocol(tp)) tp.flush()
def test_tree(): t = branch(branch( leaf("aa"), leaf("ab"), ), branch( leaf("ba"), leaf("bb"), )) transport_out = TMemoryBuffer() protocol_out = TJSONProtocol(transport_out) t.write(protocol_out) json_out = transport_out.getvalue() transport_in = TMemoryBuffer(json_out) protocol_in = TJSONProtocol(transport_in) u = Tree() u.read(protocol_in) assert t == u
def readMessageBegin(self): if hasattr(self.trans, 'getvalue'): js = self.trans.getvalue() else: js = self._readTransport() message = json.loads(js) self._ctx = self.InitContext(message) self._stack = [] return TJSONProtocol.readMessageBegin(self)
def test_profile(self): prof = Profiler() prof.start() try: Tester1().t1() finally: prof.done() self.assertNotEqual(os.path.getsize('python-profiler-result.jandy'), 0) with open("python-profiler-result.jandy", "r") as file: trans = TFileObjectTransport(file) context = ProfilingContext() context.read(TJSONProtocol(trans)) print(context) self.assertIsNotNone(context.root)
def __auth_headers(self, body): headers = dict() headers[HK_HOST] = self.host headers[HK_TIMESTAMP] = str(int(time.time() + self.__clock_offset)) headers[HK_CONTENT_MD5] = hashlib.md5(body).hexdigest() auth_header = HttpAuthorizationHeader() auth_header.algorithm = MacAlgorithm.HmacSHA1 auth_header.userType = self.credential.type auth_header.secretKeyId = self.credential.secretKeyId auth_header.signedHeaders = list(headers.iterkeys()) buf = "\n".join([headers[x] for x in auth_header.signedHeaders]) auth_header.signature = \ hmac.new(self.credential.secretKey, buf, hashlib.sha1).hexdigest() mb = TMemoryBuffer() protocol = TJSONProtocol(mb) auth_header.write(protocol) headers[HK_AUTHORIZATION] = str(mb.getvalue()) return headers
def __auth_headers(self, headers, body, support_account_key): auth_headers = dict() if self.credential and self.credential.type and self.credential.secretKeyId: if self.credential.type in SIGNATURE_SUPPORT: auth_headers[HOST] = self.host # timestamp auth_headers[TIMESTAMP] = str( int(time.time() + self.__clock_offset)) auth_headers[MI_DATE] = formatdate(usegmt=True) # content md5 auth_headers[CONTENT_MD5] = hashlib.md5(body).hexdigest() headers_to_sign = defaultdict(lambda: []) for k, v in headers.iteritems(): headers_to_sign[str(k).lower()].append(v) for k, v in auth_headers.iteritems(): headers_to_sign[str(k).lower()].append(v) signature = base64.b64encode( self.sign( self.__form_sign_content("POST", self.uri, headers_to_sign))).strip() auth_string = "Galaxy-V2 %s:%s" % (self.credential.secretKeyId, signature) auth_headers[AUTHORIZATION] = auth_string else: auth_header = HttpAuthorizationHeader() auth_header.secretKeyId = self.credential.secretKeyId auth_header.userType = self.credential.type auth_header.secretKey = self.credential.secretKey auth_header.supportAccountKey = support_account_key mb = TMemoryBuffer() protocol = TJSONProtocol(mb) auth_header.write(protocol) auth_headers[AUTHORIZATION] = str(mb.getvalue()) return auth_headers
def test_TJSONProtol_read(self): expected = "{'software':'thrift','1':[23,1.2010000000000001,32767,2147483647,9223372036854775807],'base64':'hello thrift','bool':False}" read_data = '{"software":"thrift","1":[23,1.2010000000000001,32767,2147483647,9223372036854775807],"base64":"aGVsbG8gdGhyaWZ0","bool":0}' buff = TTransport.TMemoryBuffer(read_data.encode('utf-8')) transport = TTransport.TBufferedTransportFactory().getTransport(buff) protocol = TJSONProtocol(transport) protocol.readJSONObjectStart() u_1 = protocol.readString() u_2 = protocol.readString() u_3 = protocol.readString() protocol.readJSONArrayStart() u_4 = protocol.readNumber() u_5 = protocol.readDouble() u_6 = protocol.readI16() u_7 = protocol.readI32() u_8 = protocol.readI64() protocol.readJSONArrayEnd() u_9 = protocol.readString() u_10 = protocol.readJSONBase64() u_11 = protocol.readString() u_12 = protocol.readBool() protocol.writeJSONObjectEnd() result_read = {} result_read[u_1] = u_2 result_read[u_3] = [] result_read[u_3].append(u_4) result_read[u_3].append(u_5) result_read[u_3].append(u_6) result_read[u_3].append(u_7) result_read[u_3].append(u_8) result_read[u_9] = u_10.decode('utf-8') result_read[u_11] = u_12 self.assertEqual(eval(expected), result_read)
def writeMessageEnd(self): TJSONProtocol.writeMessageEnd(self) # The thrift JSON parser is very sensitive, it can't handle spaces after # commas or colons <table flip emoji> json.dump(self._ctx.get_buffer(), self.trans, separators=(',',':'))
def writeMessageBegin(self, name, request_type, seqid): self._StartWriteContext(self.InitContext, None) TJSONProtocol.writeMessageBegin(self, name, request_type, seqid)
def __init__(self, trans): TJSONProtocol.__init__(self, trans) self._stack = [] self._ctx = None
def test_TJSONProtocol_write(self): write_data = '{"software":"thrift","1":[23,1.2010000000000001,32767,2147483647,9223372036854775807],"base64":"aGVsbG8gdGhyaWZ0","bool":0}' buff = TTransport.TMemoryBuffer() transport = TTransport.TBufferedTransportFactory().getTransport(buff) protocol = TJSONProtocol(transport) protocol.writeJSONObjectStart() protocol.writeJSONString("software") protocol.writeJSONString("thrift") protocol.writeJSONString("1") protocol.writeJSONArrayStart() protocol.writeJSONNumber(23) protocol.writeDouble(1.201) protocol.writeI16(32767) protocol.writeI32(2147483647) protocol.writeI64(9223372036854775807) protocol.writeJSONArrayEnd() protocol.writeJSONString("base64") protocol.writeJSONBase64("hello thrift".encode('utf-8')) protocol.writeJSONString("bool") protocol.writeBool(0) protocol.writeJSONObjectEnd() transport.flush() value = buff.getvalue() self.assertEqual(write_data, value.decode('utf-8'))
def writeMessageEnd(self): TJSONProtocol.writeMessageEnd(self) # The thrift JSON parser is very sensitive, it can't handle spaces after # commas or colons <table flip emoji> json.dump(self._ctx.get_buffer(), self.trans, separators=(',', ':'))