def get_protoco_factory(self): # These imports bypass the cython binary from thriftpy2.protocol.binary import TBinaryProtocolFactory from thriftpy2.transport.memory import TMemoryBuffer factory = TBinaryProtocolFactory() return lambda transport: (factory.get_protocol(TMemoryBuffer()), factory.get_protocol(transport))
def get_protoco_factory(self): # These imports bypass the cython binary from thriftpy2.protocol.binary import TBinaryProtocolFactory from thriftpy2.transport.memory import TMemoryBuffer factory = TBinaryProtocolFactory() return lambda transport: ( factory.get_protocol(TMemoryBuffer()), factory.get_protocol(transport) )
def client_context(service, host='localhost', port=9090, path='', scheme='http', proto_factory=TBinaryProtocolFactory(), trans_factory=TBufferedTransportFactory(), ssl_context_factory=None, timeout=DEFAULT_HTTP_CLIENT_TIMEOUT_MS, url=''): if url: parsed_url = urllib.parse.urlparse(url) host = parsed_url.hostname or host port = parsed_url.port or port scheme = parsed_url.scheme or scheme path = parsed_url.path or path uri = HTTP_URI.format(scheme=scheme, host=host, port=port, path=path) http_socket = THttpClient(uri, timeout, ssl_context_factory) transport = trans_factory.get_transport(http_socket) try: iprot = proto_factory.get_protocol(transport) transport.open() yield TClient(service, iprot) finally: transport.close()
def make_server(service, handler, host, port, proto_factory=TBinaryProtocolFactory()): processor = TProcessor(service, handler) server = THttpServer(processor, (host, port), iprot_factory=proto_factory) return server
class FramedTransportTestCase(TestCase): TRANSPORT_FACTORY = TFramedTransportFactory() PROTOCOL_FACTORY = TBinaryProtocolFactory() def mk_server(self): sock = self.server_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.bind(('127.0.0.1', 0)) sock.setblocking(0) self.port = sock.getsockname()[-1] self.server_thread = threading.Thread(target=self.listen) self.server_thread.setDaemon(True) self.server_thread.start() def listen(self): self.server_sock.listen(128) if asyncio: # In Tornado 5.0+, the asyncio event loop will be used # automatically by default asyncio.set_event_loop(asyncio.new_event_loop()) self.io_loop = ioloop.IOLoop.current() server = make_server(addressbook.AddressBookService, Dispatcher(self.io_loop), io_loop=self.io_loop) server.add_socket(self.server_sock) self.io_loop.start() def mk_client(self): return make_client(addressbook.AddressBookService, '127.0.0.1', self.port, proto_factory=self.PROTOCOL_FACTORY, trans_factory=self.TRANSPORT_FACTORY) def setUp(self): self.mk_server() time.sleep(0.1) self.client = self.mk_client() def tearDown(self): self.io_loop.stop() @pytest.mark.skipif(sys.version_info[:2] == (2, 6), reason="not support") def test_able_to_communicate(self): dennis = addressbook.Person(name='Dennis Ritchie') success = self.client.add(dennis) assert success success = self.client.add(dennis) assert not success @pytest.mark.skipif(sys.version_info[:2] == (2, 6), reason="not support") def test_zero_length_string(self): dennis = addressbook.Person(name='') success = self.client.add(dennis) assert success success = self.client.get(name='') assert success
def make_client(service, host, port, path='', scheme='http', proto_factory=TBinaryProtocolFactory(), trans_factory=TBufferedTransportFactory(), ssl_context_factory=None, timeout=DEFAULT_HTTP_CLIENT_TIMEOUT_MS): uri = HTTP_URI.format(scheme=scheme, host=host, port=port, path=path) http_socket = THttpClient(uri, timeout, ssl_context_factory) transport = trans_factory.get_transport(http_socket) iprot = proto_factory.get_protocol(transport) transport.open() return TClient(service, iprot)