Esempio n. 1
0
    def _get_client_by_transport(self, options, transport, socket=None):
        # Create the protocol and client
        if options.json:
            protocol = TJSONProtocol.TJSONProtocol(transport)
        elif options.compact:
            protocol = TCompactProtocol.TCompactProtocol(transport)

        # No explicit option about protocol is specified. Try to infer.
        elif options.framed or options.unframed:
            protocol = TBinaryProtocol.TBinaryProtocolAccelerated(transport)

        elif socket is not None:
            # If json, compact, framed, and unframed are not specified,
            # THeaderProtocol is the default. Create a protocol using either
            # fuzzy or non-fuzzy transport depending on if options.fuzz is set.
            if options.fuzz is not None:
                transport = TFuzzyHeaderTransport(socket,
                                                  fuzz_fields=options.fuzz,
                                                  verbose=True)
            else:
                transport = THeaderTransport(socket)
            protocol = THeaderProtocol.THeaderProtocol(transport)
        else:
            self._exit(error_message=('No valid protocol '
                                      'specified for %s' % (type(self))),
                       status=os.EX_USAGE)

        transport.open()
        self._transport = transport

        client = self.service_class.Client(protocol)
        return client
    def test_throw_populates_headers(self):
        handler = self.Handler({7: "hello"})
        processor = TestService.Processor(handler)
        server = TCppServerTestManager.make_server(processor)
        with TCppServerTestManager(server) as server:
            host, port = server.addr()
            with TSocket.TSocket(host=host, port=port) as sock:
                transport = THeaderTransport.THeaderTransport(sock)
                protocol = THeaderProtocol.THeaderProtocol(transport)
                client = TestService.Client(protocol, protocol)

                try:
                    client.throwUserException()
                    self.fail("Expect to throw UserException2")
                except UserException2:
                    pass

                self.assertEquals(b"UserException2",
                                  transport.get_headers()[b"uex"])
                self.assertIn(b"Some message",
                              transport.get_headers()[b"uexw"])

                try:
                    client.throwUncaughtException("a message!")
                    self.fail("Expect to throw TApplicationException")
                except TApplicationException:
                    pass

                self.assertEquals(b"TApplicationException",
                                  transport.get_headers()[b"uex"])
                self.assertIn(b"a message!", transport.get_headers()[b"uexw"])
Esempio n. 3
0
 def _perform_rpc(self, server, val):
     host, port = server.addr()
     with TSocket.TSocket(host=host, port=port) as sock:
         transport = THeaderTransport.THeaderTransport(sock)
         protocol = THeaderProtocol.THeaderProtocol(transport)
         client = SlotsThrowingService.Client(protocol, protocol)
         return client.getDataById(val)
Esempio n. 4
0
 def __init__(self, host: str,
              transport: THeaderTransport.THeaderTransport) -> None:
     self.host = host  # Just for accessibility
     self._transport = transport
     self._transport.add_transform(THeaderTransport.TRANSFORM.ZSTD)
     OpenrCtrl.Client.__init__(
         self, THeaderProtocol.THeaderProtocol(self._transport))
Esempio n. 5
0
    def setUp(self):
        if options.http_path:
            self.transport = THttpClient.THttpClient(
                options.host, options.port, options.http_path)
        else:
            socket = TSocket.TSocket(options.host, options.port)

            # frame or buffer depending upon args
            if options.framed:
                self.transport = TTransport.TFramedTransport(socket)
            else:
                self.transport = TTransport.TBufferedTransport(socket)

        protocol = self.protocol_factory.getProtocol(self.transport)
        if options.header:
            protocol = THeaderProtocol.THeaderProtocol(socket)
            self.transport = protocol.trans
            protocol.trans.add_transform(protocol.trans.ZLIB_TRANSFORM)

        self.transport.open()
        self.client = ThriftTest.Client(protocol)
        if options.multiple:
            p = TMultiplexedProtocol.TMultiplexedProtocol(protocol,
                    "ThriftTest")
            self.client = ThriftTest.Client(p)
            p = TMultiplexedProtocol.TMultiplexedProtocol(protocol,
                    "SecondService")
            self.client2 = SecondService.Client(p)
        else:
            self.client = ThriftTest.Client(protocol)
            self.client2 = None
Esempio n. 6
0
def getHeaderClient(addr, sock_cls=TSocket.TSocket):
    transport = sock_cls(addr[0], addr[1])
    transport = THeaderTransport.THeaderTransport(transport)
    transport.set_header("hello", "world")
    protocol = THeaderProtocol.THeaderProtocol(transport)
    client = SleepService.Client(protocol)
    transport.open()
    return client
Esempio n. 7
0
def _make_protocol(endpoint):
    if endpoint.family == socket.AF_INET:
        trans = TSocket.TSocket(*endpoint.address)
    elif endpoint.family == socket.AF_UNIX:
        trans = TSocket.TSocket(unix_socket=endpoint.address)
    else:
        raise Exception("unsupported endpoint family %r" % endpoint.family)
    return THeaderProtocol.THeaderProtocol(trans)
 def _expiring_rpc(self, server, service, method, tm, *args, **kwargs):
     host, port = server.addr()
     with TSocket.TSocket(host=host, port=port) as sock:
         sock.setTimeout(tm)
         transport = THeaderTransport.THeaderTransport(sock)
         protocol = THeaderProtocol.THeaderProtocol(transport)
         client = service.Client(protocol, protocol)
         return getattr(client, method)(*args, **kwargs)
Esempio n. 9
0
    def testHeader(self):
        transport = TSocket.TSocket(self.server_addr[0], self.server_addr[1])
        transport = THeaderTransport.THeaderTransport(transport)
        transport.set_header("hello", "world")
        protocol = THeaderProtocol.THeaderProtocol(transport)
        client = SleepService.Client(protocol)
        transport.open()

        self.assertEquals(client.space("hi"), "h i")
        self.stopServer()
Esempio n. 10
0
    def _get_client_by_transport(self, config, transport, socket=None):
        # Create the protocol and client
        if config.compact:
            protocol = TCompactProtocol.TCompactProtocol(transport)
        # No explicit option about protocol is specified. Try to infer.
        elif config.framed or config.unframed:
            protocol = TBinaryProtocol.TBinaryProtocolAccelerated(transport)
        elif socket is not None:
            protocol = THeaderProtocol.THeaderProtocol(socket)
            transport = protocol.trans
        else:
            raise ValueError("No protocol specified for HTTP Transport")
        transport.open()
        self._transport = transport

        client = self.client_class(protocol)
        return client
Esempio n. 11
0
    def _get_client_by_transport(self, options, transport, socket=None):
        # Create the protocol and client
        if options.json:
            protocol = TJSONProtocol.TJSONProtocol(transport)
        elif options.compact:
            protocol = TCompactProtocol.TCompactProtocol(transport)

        # No explicit option about protocol is specified. Try to infer.
        elif options.framed or options.unframed:
            protocol = TBinaryProtocol.TBinaryProtocolAccelerated(transport)

        elif socket is not None:
            # If json, compact, framed, and unframed are not specified,
            # THeaderProtocol is the default. Create a protocol using either
            # fuzzy or non-fuzzy transport depending on if options.fuzz is set.
            if options.fuzz is not None:
                transport = TFuzzyHeaderTransport(socket,
                                                  fuzz_fields=options.fuzz,
                                                  verbose=True)
            else:
                transport = THeaderTransport(socket)
                if options.headers is not None:
                    try:
                        parsed_headers = eval(options.headers)
                    except Exception:
                        self._exit(
                            error_message='Request headers (--headers) argument'
                            ' failed eval')
                    if not isinstance(parsed_headers, dict):
                        self._exit(
                            error_message='Request headers (--headers) argument'
                            ' must evaluate to a dict')
                    for header_name, header_value in parsed_headers.items():
                        transport.set_header(header_name, header_value)
            protocol = THeaderProtocol.THeaderProtocol(transport)
        else:
            self._exit(error_message=('No valid protocol '
                                      'specified for %s' % (type(self))),
                       status=os.EX_USAGE)

        transport.open()
        self._transport = transport

        client = self.service_class.Client(protocol)

        return client
Esempio n. 12
0
    def get_thrift_client(self, use_ssl):
        socket = (
            TSSLSocket.TSSLSocket(
                host=self.cli_opts.host,
                port=self.cli_opts.openr_ctrl_port,
                # verify server
                cert_reqs=ssl.CERT_REQUIRED,
                ca_certs=self.cli_opts.ca_file,
                certfile=self.cli_opts.cert_file,
                keyfile=self.cli_opts.key_file,
                verify_name=self.cli_opts.acceptable_peer_name,
            ) if use_ssl else TSocket.TSocket(
                host=self.cli_opts.host, port=self.cli_opts.openr_ctrl_port))
        socket.setTimeout(self.cli_opts.timeout)
        transport = THeaderTransport.THeaderTransport(socket)
        protocol = THeaderProtocol.THeaderProtocol(transport)

        transport.open()
        return OpenrCtrl.Client(protocol)
Esempio n. 13
0
    def _get_client_by_transport(self, options, transport, socket=None):
        # Create the protocol and client
        if options.json:
            protocol = TJSONProtocol.TJSONProtocol(transport)
        elif options.compact:
            protocol = TCompactProtocol.TCompactProtocol(transport)

        # No explicit option about protocol is specified. Try to infer.
        elif options.framed or options.unframed:
            protocol = TBinaryProtocol.TBinaryProtocolAccelerated(transport)

        elif socket is not None:
            # If json, compact, framed, and unframed are not specified,
            # THeaderProtocol is the default.
            transport = THeaderTransport(socket)
            if options.headers is not None:
                try:
                    parsed_headers = eval(options.headers)
                except Exception:
                    self._exit(
                        error_message="Request headers (--headers) argument"
                        " failed eval")
                if not isinstance(parsed_headers, dict):
                    self._exit(
                        error_message="Request headers (--headers) argument"
                        " must evaluate to a dict")
                for header_name, header_value in parsed_headers.items():
                    transport.set_header(header_name, header_value)
            protocol = THeaderProtocol.THeaderProtocol(transport)
        else:
            self._exit(
                error_message=("No valid protocol "
                               "specified for %s" % (type(self))),
                status=os.EX_USAGE,
            )

        transport.open()
        self._transport = transport

        client = self.service_class.Client(protocol)

        return client
Esempio n. 14
0
    def _get_client_by_transport(self, options, transport, socket=None):
        # Create the protocol and client
        if options.json:
            protocol = TJSONProtocol.TJSONProtocol(transport)
        elif options.compact:
            protocol = TCompactProtocol.TCompactProtocol(transport)

        # No explicit option about protocol is specified. Try to infer.
        elif options.framed or options.unframed:
            protocol = TBinaryProtocol.TBinaryProtocolAccelerated(transport)
        else:
            if socket is None:
                self._exit(error_message=('No valid protocol '
                                          'specified for %s' % (type(self))),
                           status=os.EX_USAGE)
            else:
                protocol = THeaderProtocol.THeaderProtocol(socket)
                transport = protocol.trans
        transport.open()
        self._transport = transport

        client = self.service_class.Client(protocol)
        return client
Esempio n. 15
0
def create_client(port):
    socket = TSocket.TSocket("localhost", port)
    protocol = THeaderProtocol.THeaderProtocol(socket)
    protocol.trans.set_max_frame_size(MAX_BIG_FRAME_SIZE)
    protocol.trans.open()
    return ThriftTest.Client(protocol)