Beispiel #1
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)
Beispiel #2
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)
Beispiel #3
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)
Beispiel #4
0
    def stringReceived(self, frame):
        tmi = TTransport.TMemoryBuffer(frame)
        tmo = TTransport.TMemoryBuffer()

        iprot = self.factory.iprot_factory.getProtocol(tmi)
        oprot = self.factory.oprot_factory.getProtocol(tmo)

        server_ctx = TwistedRpcConnectionContext(self.transport.socket)
        d = self.factory.processor.process(iprot, oprot, server_ctx)
        d.addCallbacks(self.processOk, self.processError,
            callbackArgs=(tmo,))
Beispiel #5
0
 def __init__(self, *args):
     if (len(args) == 2):
         self.__initArgs__(args[0], args[1],
                           TTransport.TTransportFactoryBase(),
                           TTransport.TTransportFactoryBase(),
                           TBinaryProtocol.TBinaryProtocolFactory(),
                           TBinaryProtocol.TBinaryProtocolFactory())
     elif (len(args) == 4):
         self.__initArgs__(args[0], args[1], args[2], args[2], args[3],
                           args[3])
     elif (len(args) == 6):
         self.__initArgs__(args[0], args[1], args[2], args[3], args[4],
                           args[5])
Beispiel #6
0
    def handle(self):
        """Handle requests.

        WARNING! You must call prepare BEFORE calling handle.
        """
        assert self.prepared, "You have to call prepare before handle"
        rset, wset, xset = self._select()
        for readable in rset:
            if readable == self._read.fileno():
                # don't care i just need to clean readable flag
                self._read.recv(1024)
            elif readable in self.socket.handles:
                client_socket = self.socket.accept()
                connection = Connection(client_socket, self)
                self.clients[client_socket.fileno()] = connection
            else:
                connection = self.clients[readable]
                connection.read()
                if connection.status == WAIT_PROCESS:
                    itransport = TTransport.TMemoryBuffer(connection.message)

                    # Header protocol needs oprot == iprot. This implies the
                    # input memory buffer is reused for output too.
                    if isinstance(self.in_protocol, THeaderProtocolFactory):
                        omembuf = itransport
                        iprot = self.in_protocol.getProtocol(itransport)
                        oprot = iprot
                    else:
                        # Otherwise, assume we need a TFramedTransport.
                        omembuf = TTransport.TMemoryBuffer()
                        itransport = TTransport.TFramedTransport(itransport)
                        otransport = TTransport.TFramedTransport(omembuf)
                        iprot = self.in_protocol.getProtocol(itransport)
                        oprot = self.out_protocol.getProtocol(otransport)

                    if self.max_queue_size == 0 or \
                            self.tasks.qsize() <= self.max_queue_size:
                        self.tasks.put([
                            self.processor, iprot, oprot, omembuf, connection
                        ])
                    else:
                        logging.error(
                            "Queue max size of %d exceeded. Request rejected.",
                            self.max_queue_size)
        for writeable in wset:
            self.clients[writeable].write()
        for oob in xset:
            if oob in self.clients:
                connection = self.clients[oob]
                connection.close()
Beispiel #7
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
Beispiel #8
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)
Beispiel #9
0
 def connectionLost(self, reason=connectionDone):
     if sys.version_info[0] >= 3:
         client_req_iter = self.client._reqs.items()
     else:
         client_req_iter = self.client._reqs.items()
     for _, v in client_req_iter:
         tex = TTransport.TTransportException(
             type=TTransport.TTransportException.END_OF_FILE,
             message=self._errormsg or 'Connection closed')
         v.errback(tex)
Beispiel #10
0
def serialize(protocol_factory, thr):
    # type: (Any, Any) -> AnyStr
    """Convenience method for serializing objects using the given
    protocol factory and a TMemoryBuffer."""
    transport = TTransport.TMemoryBuffer()
    protocol = protocol_factory.getProtocol(transport)
    thr.write(protocol)
    if isinstance(protocol, THeaderProtocol.THeaderProtocol):
        protocol.trans.flush()
    return transport.getvalue()
Beispiel #11
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
Beispiel #12
0
    def stringReceived(self, frame):
        tr = TTransport.TMemoryBuffer(frame)
        iprot = self._iprot_factory.getProtocol(tr)
        (fname, mtype, rseqid) = iprot.readMessageBegin()

        try:
            method = self.recv_map[fname]
        except KeyError:
            method = getattr(self.client, 'recv_' + fname)
            self.recv_map[fname] = method

        method(iprot, mtype, rseqid)
Beispiel #13
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
Beispiel #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
Beispiel #15
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
Beispiel #16
0
def deserialize(protocol_factory, data, thr_out):
    # type: (Any, AnyStr, T) -> T
    """Convenience method for deserializing objects using the given
    protocol factory and a TMemoryBuffer.  returns its thr_out
    argument."""
    transport = TTransport.TMemoryBuffer(data)
    try:
        protocol = protocol_factory.getProtocol(transport, thr_out.thrift_spec)  # noqa: T484
    except TypeError:
        protocol = protocol_factory.getProtocol(transport)
    if isinstance(protocol, THeaderProtocol.THeaderProtocol):
        # this reads the THeader headers to detect what the underlying
        # protocol is, as well as looking at transforms, etc.
        protocol.trans.readFrame(0)
        protocol.reset_protocol()
    thr_out.read(protocol)  # noqa: T484
    return thr_out
    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)
Beispiel #18
0
def __thrift_to_json(x):
    trans = TTransport.TMemoryBuffer()
    proto = TSimpleJSONProtocol.TSimpleJSONProtocol(trans)
    x.write(proto)
    return json.loads(trans.getvalue())