示例#1
0
    def get_protoco_factory(self):
        # These imports bypass the cython binary
        from thriftpy.protocol.binary import TBinaryProtocolFactory
        from thriftpy.transport.memory import TMemoryBuffer

        factory = TBinaryProtocolFactory()
        return lambda transport: (
            factory.get_protocol(TMemoryBuffer()),
            factory.get_protocol(transport)
        )
示例#2
0
def _build_protocol_factory(protocol):
    if protocol == Protocol.binary:
        return TBinaryProtocolFactory()
    elif protocol == Protocol.compact:
        return TCompactProtocolFactory()
    else:
        raise Exception("Unknown protocol")
def deserialize(base,
                buf,
                protocol_factory=TBinaryProtocolFactory()):
    transport = TMemoryBuffer(buf)
    protocol = protocol_factory.get_protocol(transport)
    base.read(protocol)
    return base
示例#4
0
文件: http.py 项目: tubular/thriftpy
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
示例#5
0
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
示例#6
0
def test(thrift_server, thriftpy_test_module, request_xtruct):
    client = make_client(thriftpy_test_module.ThriftTest,
                         host='127.0.0.1',
                         port=6000,
                         proto_factory=TBinaryProtocolFactory(),
                         trans_factory=TFramedTransportFactory())
    response = client.testStruct(request_xtruct)
    assert response == utils.build_xtruct(thriftpy_test_module,
                                          "string_thingy2", 1, 17, 33, 65)
示例#7
0
def init_broker():
  try:
    global client
    client = make_client(broker_thrift.TradeService, config.BROKER_HOST, config.BROKER_PORT,
                           proto_factory=TBinaryProtocolFactory(),
                           trans_factory=TFramedTransportFactory(),
                           timeout=60000)  
  except Exception as e:
    logging.warn("make_client exception")
    traceback.print_exc()
示例#8
0
class FramedTransportTestCase(TestCase):
    TRANSPORT_FACTORY = TFramedTransportFactory()
    PROTOCOL_FACTORY = TBinaryProtocolFactory()

    def mk_server(self):
        self.io_loop = ioloop.IOLoop()
        server = make_server(addressbook.AddressBookService,
                             Dispatcher(self.io_loop),
                             io_loop=self.io_loop)

        self.server = server
        sock = self.server_sock = socket.socket(socket.AF_INET,
                                                socket.SOCK_STREAM)
        sock.bind(('localhost', 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)
        self.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 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

    def test_zero_length_string(self):
        dennis = addressbook.Person(name='')
        success = self.client.add(dennis)
        assert success
        success = self.client.get(name='')
        assert success
示例#9
0
文件: http.py 项目: tubular/thriftpy
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)
class BufferedTransportTestCase(TestCase):
    TRANSPORT_FACTORY = TBufferedTransportFactory()
    PROTOCOL_FACTORY = TBinaryProtocolFactory()

    PORT = 50001

    def mk_server(self):
        server = make_server(addressbook.AddressBookService,
                             Dispatcher(),
                             host="localhost",
                             port=self.PORT,
                             proto_factory=self.PROTOCOL_FACTORY,
                             trans_factory=self.TRANSPORT_FACTORY)
        p = multiprocessing.Process(target=server.serve)
        return p

    def client(self):
        return client_context(addressbook.AddressBookService,
                              host="localhost",
                              port=self.PORT,
                              proto_factory=self.PROTOCOL_FACTORY,
                              trans_factory=self.TRANSPORT_FACTORY)

    def setUp(self):
        self.server = self.mk_server()
        self.server.start()
        time.sleep(0.3)

    def tearDown(self):
        if self.server.is_alive():
            self.server.terminate()

    def test_able_to_communicate(self):
        dennis = addressbook.Person(name='Dennis Ritchie')
        with self.client() as c:
            success = c.add(dennis)
            assert success

            success = c.add(dennis)
            assert not success

    def test_zero_length_string(self):
        dennis = addressbook.Person(name='')
        with self.client() as c:
            success = c.add(dennis)
            assert success
            success = c.get(name='')
            assert success
def serialize(thrift_object, protocol_factory=TBinaryProtocolFactory()):
    transport = TMemoryBuffer()
    protocol = protocol_factory.get_protocol(transport)
    thrift_object.write(protocol)
    return transport.getvalue()