Exemplo n.º 1
0
 def __init__(self,
              processor,
              lsocket,
              inputProtocolFactory=None,
              outputProtocolFactory=None,
              threads=10,
              readTimeout=None,
              maxQueueSize=0):
     self.processor = self._getProcessor(processor)
     self.socket = lsocket
     self.in_protocol = inputProtocolFactory or TBinaryProtocolFactory()
     self.out_protocol = outputProtocolFactory or self.in_protocol
     self.threads = int(threads)
     self.clients = {}
     self.max_queue_size = maxQueueSize  # do not set this as a hard size
     # maximum - the queue may need
     # extra space for close()
     self.tasks = Queue.Queue()
     self._read, self._write = _create_socketpair()
     self.prepared = False
     self._stop = False
     self.serverEventHandler = TServer.TServerEventHandler()
     self.select_timeout = DEFAULT_SELECT_TIMEOUT
     self.poller = TSocket.ConnectionEpoll() if hasattr(select, "epoll") \
             else TSocket.ConnectionSelect()
     self.last_logged_error = 0
     timeouts = [x for x in [self.select_timeout, readTimeout] \
                     if x is not None]
     if len(timeouts) > 0:
         self.select_timeout = min(timeouts)
     self._readTimeout = readTimeout
Exemplo n.º 2
0
    def test_bad_port(self):
        port = 'bogus'
        with self.assertRaises(ValueError):
            with TSocket.TServerSocket(port=port):
                pass

        with self.assertRaises(ValueError):
            with TSocket.TSocket(port=port):
                pass
Exemplo n.º 3
0
 def test_unix_socket(self):
     text = "hi"  # sample text to send over the wire
     with tempfile.NamedTemporaryFile(delete=True) as fh:
         unix_socket = fh.name
         with TSocket.TServerSocket(unix_socket=unix_socket) as server:
             with TSocket.TSocket(unix_socket=unix_socket) as conn:
                 conn.write(text)
             with server.accept() as client:
                 read = client.read(len(text))
             self.assertEquals(read, text)
         # The socket will not be cleaned up when the server has been shutdown.
         self.assertTrue(os.path.exists(unix_socket))
Exemplo n.º 4
0
 def test_deprecated_str_form_of_port(self):
     # Make sure that the deprecated form of the `port` parameter is
     # accepted in TServerSocket and TSocket.
     port = "0"
     text = "hi"  # sample text to send over the wire
     # NB: unfortunately unittest.TestCase.assertWarns isn't available until
     # py3.
     with TSocket.TServerSocket(port=port,
                                family=socket.AF_INET6) as server:
         addr = server.getSocketNames()[0]
         with TSocket.TSocket(host=addr[0], port=str(addr[1])) as conn:
             conn.write(text)
         with server.accept() as client:
             read = client.read(len(text))
         self.assertEquals(read, text)
Exemplo n.º 5
0
    def test_open_failure(self):
        # Bind a server socket to an address, but don't actually listen on it.
        server_socket = socket.socket(socket.AF_INET6)
        try:
            server_socket.bind(('::', 0))
            server_port = server_socket.getsockname()[1]

            # Explicitly use "localhost" as the hostname, so that the
            # connect code will try both IPv6 and IPv4.  We want to
            # exercise the failure behavior when trying multiple addresses.
            sock = TSocket.TSocket(host='localhost', port=server_port)
            sock.setTimeout(50)  # ms
            try:
                sock.open()
                self.fail('unexpectedly succeeded to connect to closed socket')
            except TTransport.TTransportException:
                # sock.open() should not leave the file descriptor open
                # when it fails
                self.assertEquals(None, sock.handle)
                self.assertEquals({}, sock.handles)

                # Calling close() again on the socket should be a no-op,
                # and shouldn't throw an error
                sock.close()
        finally:
            server_socket.close()
Exemplo n.º 6
0
    def open_SSL(self, ip, port, timeout, ssl_config=None):
        """open the SSL connection

        :param ip: the server ip
        :param port: the server port
        :param timeout: the timeout for connect and execute
        :ssl_config: configs for SSL
        :return: void
        """
        self._ip = ip
        self._port = port
        self._timeout = timeout
        try:
            if ssl_config is not None:
                s = TSSLSocket.TSSLSocket(
                    self._ip, self._port, ssl_config.unix_socket,
                    ssl_config.ssl_version, ssl_config.cert_reqs,
                    ssl_config.ca_certs, ssl_config.verify_name,
                    ssl_config.keyfile, ssl_config.certfile,
                    ssl_config.allow_weak_ssl_versions)
            else:
                s = TSocket.TSocket(self._ip, self._port)
            if timeout > 0:
                s.setTimeout(timeout)
            transport = TTransport.TBufferedTransport(s)
            protocol = TBinaryProtocol.TBinaryProtocol(transport)
            transport.open()
            self._connection = GraphService.Client(protocol)
            resp = self._connection.verifyClientVersion(
                VerifyClientVersionReq())
            if resp.error_code != ErrorCode.SUCCEEDED:
                self._connection._iprot.trans.close()
                raise ClientServerIncompatibleException(resp.error_msg)
        except Exception:
            raise
Exemplo n.º 7
0
 def test_usage_as_context_manager(self):
     """
     Asserts that both TSocket and TServerSocket can be used with `with` and
     that their resources are disposed of at the close of the `with`.
     """
     text = "hi"  # sample text to send over the wire
     with TSocket.TServerSocket(port=0, family=socket.AF_INET6) as server:
         addr = server.getSocketNames()[0]
         with TSocket.TSocket(host=addr[0], port=addr[1]) as conn:
             conn.write(text)
         self.assertFalse(conn.isOpen())
         with server.accept() as client:
             read = client.read(len(text))
         self.assertFalse(conn.isOpen())
     self.assertFalse(server.isListening())
     self.assertEquals(read, text)
Exemplo n.º 8
0
    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("UserException2",
                                  transport.get_headers()["uex"])
                self.assertIn("Some message", transport.get_headers()["uexw"])

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

                self.assertEquals("TApplicationException",
                                  transport.get_headers()["uex"])
                self.assertIn("a message!", transport.get_headers()["uexw"])
Exemplo n.º 9
0
 def _get_client(self, options):
     socket = TSocket.TSocket(unix_socket=options.path)
     if options.framed:
         transport = TTransport.TFramedTransport(socket)
     else:
         transport = TTransport.TBufferedTransport(socket)
     return self._get_client_by_transport(options, transport, socket=socket)
Exemplo n.º 10
0
def establish_a_rare_connection(pytestconfig):
    addr = pytestconfig.getoption("address")
    host_addr = addr.split(":") if addr else ["localhost", get_ports()[0]]
    socket = TSocket.TSocket(host_addr[0], host_addr[1])
    transport = TTransport.TBufferedTransport(socket)
    protocol = TBinaryProtocol.TBinaryProtocol(transport)
    transport.open()
    return GraphService.Client(protocol)
Exemplo n.º 11
0
 def _get_client(self, options):
     host, port = self._parse_host_port(options.host, self.default_port)
     socket = (TSSLSocket.TSSLSocket(host, port) if options.ssl
               else TSocket.TSocket(host, port))
     if options.framed:
         transport = TTransport.TFramedTransport(socket)
     else:
         transport = TTransport.TBufferedTransport(socket)
     return self._get_client_by_transport(options, transport, socket=socket)
Exemplo n.º 12
0
 def _get_client_by_host(self):
     config = self.config
     host, port = self._parse_host_port(config.host, self.default_port)
     socket = (TSSLSocket.TSSLSocket(host, port) if config.ssl
               else TSocket.TSocket(host, port))
     if config.framed:
         transport = TTransport.TFramedTransport(socket)
     else:
         transport = TTransport.TBufferedTransport(socket)
     return self._get_client_by_transport(config, transport, socket=socket)
Exemplo n.º 13
0
 def get_connection(ip, port):
     try:
         socket = TSocket.TSocket(ip, port)
         transport = TTransport.TBufferedTransport(socket)
         protocol = TBinaryProtocol.TBinaryProtocol(transport)
         transport.open()
         connection = GraphService.Client(protocol)
     except Exception as ex:
         assert False, 'Create connection to {}:{} failed'.format(
             ip, port)
     return connection
Exemplo n.º 14
0
 def open(self):
     try:
         self.close()
         s = TSocket.TSocket(self._address.host, self._address.port)
         if self._timeout > 0:
             s.setTimeout(self._timeout)
         transport = TTransport.TBufferedTransport(s)
         protocol = TBinaryProtocol.TBinaryProtocol(transport)
         transport.open()
         self._connection = GraphStorageService.Client(protocol)
     except Exception:
         raise
Exemplo n.º 15
0
 def open(self):
     try:
         self.close()
         s = TSocket.TSocket(self._leader[0], self._leader[1])
         if self._timeout > 0:
             s.setTimeout(self._timeout)
         transport = TTransport.TBufferedTransport(s)
         protocol = TBinaryProtocol.TBinaryProtocol(transport)
         transport.open()
         self._connection = MetaService.Client(protocol)
     except Exception:
         raise
Exemplo n.º 16
0
 def _expiring_rpc(self, server, service, method, tm, headers, *args,
                   **kwargs):
     host, port = server.addr()
     with TSocket.TSocket(host=host, port=port) as sock:
         sock.setTimeout(tm)
         transport = THeaderTransport.THeaderTransport(sock)
         if headers:
             for key, val in headers.items():
                 transport.set_header(key, val)
         protocol = THeaderProtocol.THeaderProtocol(transport)
         client = service.Client(protocol, protocol)
         return getattr(client, method)(*args, **kwargs)
Exemplo n.º 17
0
 def open(self, ip, port, timeout):
     self._ip = ip
     self._port = port
     try:
         s = TSocket.TSocket(self._ip, self._port)
         if timeout > 0:
             s.setTimeout(timeout)
         transport = TTransport.TBufferedTransport(s)
         protocol = TBinaryProtocol.TBinaryProtocol(transport)
         transport.open()
         self._connection = GraphService.Client(protocol)
     except Exception:
         raise
Exemplo n.º 18
0
    def test_poller_process(self):
        # Make sure that pollers do not fail when they're given None as timeout
        text = "hi"  # sample text to send over the wire
        with TSocket.TServerSocket(port=0, family=socket.AF_INET6) as server:
            addr = server.getSocketNames()[0]

            def write_data():
                # delay writing to verify that poller.process is waiting
                time.sleep(1)
                with TSocket.TSocket(host=addr[0], port=addr[1]) as conn:
                    conn.write(text)

            poller = TSocket.ConnectionSelect()
            thread = threading.Thread(target=write_data)
            thread.start()
            for filenos in server.handles.keys():
                poller.read(filenos)

            r, _, x = poller.process(timeout=None)

            thread.join()
            # Verify that r is non-empty
            self.assertTrue(r)
Exemplo n.º 19
0
    def open(self, ip, port, timeout):
        """open the connection

        :param ip: the server ip
        :param port: the server port
        :param timeout: the timeout for connect and execute
        :return: void
        """
        self._ip = ip
        self._port = port
        self._timeout = timeout
        s = TSocket.TSocket(self._ip, self._port)
        if timeout > 0:
            s.setTimeout(timeout)
        transport = TTransport.TBufferedTransport(s)
        protocol = TBinaryProtocol.TBinaryProtocol(transport)
        transport.open()
        self._connection = GraphService.Client(protocol)
        resp = self._connection.verifyClientVersion(VerifyClientVersionReq())
        if resp.error_code != ErrorCode.SUCCEEDED:
            self._connection._iprot.trans.close()
            raise ClientVersionRejectedException(resp.error_msg)
Exemplo n.º 20
0
 def do_test():
     with TSocket.TServerSocket(port=0, family=socket.AF_INET6):
         raise Exception('test_error')
Exemplo n.º 21
0
 def write_data():
     # delay writing to verify that poller.process is waiting
     time.sleep(1)
     with TSocket.TSocket(host=addr[0], port=addr[1]) as conn:
         conn.write(text)