Beispiel #1
0
 def __init__(self,
              host=None,
              port=9090,
              certfile='cert.pem',
              unix_socket=None):
     self.setCertfile(certfile)
     TServerSocket.__init__(self, host, port)
Beispiel #2
0
def main():
    """
  main() loop, called from jobsubd management command.
  """
    LOG.info("Starting daemon on port %s" % PORT)
    sock = TServerSocket(PORT)
    sock.host = HOST
    TThreadedServer(JobSubmissionService.Processor(JobSubmissionServiceImpl()),
                    sock, TBufferedTransportFactory(),
                    TBinaryProtocolFactory()).serve()
Beispiel #3
0
def main():
  """
  main() loop, called from jobsubd management command.
  """
  LOG.info("Starting daemon on port %s" % PORT)
  sock = TServerSocket(PORT)
  sock.host = HOST
  TThreadedServer(JobSubmissionService.Processor(JobSubmissionServiceImpl()),
    sock,
    TBufferedTransportFactory(),
    TBinaryProtocolFactory()).serve()
Beispiel #4
0
 def _create_server(self, port=0):
     handler = CalculatorHandler()
     transport = TServerSocket(port)
     pfactory = THeaderProtocolFactory(client_types=THEADER_CLIENT_TYPES)
     return TNonblockingServer(
         Calculator.Processor(handler),
         transport,
         inputProtocolFactory=pfactory,
         outputProtocolFactory=pfactory,
         threads=1,
     )
Beispiel #5
0
    def initTask(self):
        """Overridden to bind sockets, etc"""
        super(NBServerTask, self).initTask()

        self._stopped = False
        self.socket = TServerSocket(self.host, self.port)
        self.server = TNonblockingServer(self.processor, self.socket,
                                         threads=self.num_threads)
        self.server.prepare()
        self.bound_host, self.bound_port = \
            self.server.socket.handle.getsockname()
        self.logger.info("%s Server Started on %s:%s",
                         self.name, self.bound_host, self.bound_port)
Beispiel #6
0
    def do_run(self):
        if settings.USE_SSL:
            socket = TLateInitSSLServerSocket(
                port=settings.THRIFT_PORT, certfile=settings.SSL_CERTIFICATE)
        else:
            socket = TServerSocket(port=settings.THRIFT_PORT)


#        server = TThreadedServer(ThriftProcessor(), socket, TFramedTransportFactory(), TBinaryProtocolFactory())
        self.serverpid = os.getpid()
        self.server = TForkingServer(ThriftProcessor(), socket,
                                     TFramedTransportFactory(),
                                     TBinaryProtocolFactory())
        self.server.serve()
Beispiel #7
0
    def test_isOpen_checks_for_readability(self):
        # https://docs.python.org/3/library/socket.html#notes-on-socket-timeouts
        # https://docs.python.org/3/library/socket.html#socket.socket.settimeout
        timeouts = [
            None,  # blocking mode
            0,  # non-blocking mode
            1.0,  # timeout mode
        ]

        for timeout in timeouts:
            acc = ServerAcceptor(TServerSocket(port=0))
            acc.start()

            sock = TSocket(host="localhost", port=acc.port)
            self.assertFalse(sock.isOpen())
            sock.open()
            sock.setTimeout(timeout)

            # the socket shows as open immediately after connecting
            self.assertTrue(sock.isOpen())

            # and remains open during usage
            sock.write(b"hello")
            self.assertTrue(sock.isOpen())
            while True:
                try:
                    sock.read(5)
                except TTransportException as exc:
                    if exc.inner.errno == errno.EAGAIN:
                        # try again when we're in non-blocking mode
                        continue
                    raise
                break
            self.assertTrue(sock.isOpen())

            # once the server side closes, it no longer shows open
            acc.client.close(
            )  # this also blocks until the other thread is done
            acc.close()
            self.assertFalse(sock.isOpen())

            sock.close()
Beispiel #8
0
    def initTask(self):
        """Overridden to bind sockets, etc"""
        super(NBServerTask, self).initTask()

        self._stopped = False

        # Construct TServerSocket this way for compatibility with fbthrift
        self.socket = TServerSocket(port=self.port)
        self.socket.host = self.host

        self.server = TNonblockingServer(self.processor, self.socket,
                                         threads=self.num_threads)
        self.server.prepare()

        self.bound_addrs = []
        for handle in self._get_socket_handles(self.server.socket):
            addrinfo = handle.getsockname()
            self.bound_host, self.bound_port = addrinfo[0:2]
            self.logger.info("%s Server Started on %s", self.name,
                self._fmt_hostport(self.bound_host, self.bound_port))
    def test_it_works(self):
        processor_factory = mock.Mock()
        mock_processor = mock.Mock()
        processor_factory.get_processor.return_value = mock_processor
        proto_factory = FProtocolFactory(TJSONProtocolFactory())
        server_trans = TServerSocket(host='localhost', port=5536)
        server = FSimpleServer(processor_factory, server_trans, proto_factory)

        thread = Thread(target=lambda: server.serve())
        thread.start()
        time.sleep(0.1)

        transport = TSocket(host='localhost', port=5536)
        transport.open()
        transport.write(bytearray([0, 0, 0, 3, 1, 2, 3]))
        transport.flush()
        time.sleep(0.1)

        server.stop()
        processor_factory.get_processor.assert_called_once_with(mock.ANY)
        mock_processor.process.assert_called_with(mock.ANY, mock.ANY)
Beispiel #10
0
sys.path.append(
    os.path.abspath(os.path.dirname(os.path.abspath(
        os.path.dirname(__file__)))))
sys.path.append(
    os.path.abspath(
        os.path.abspath(os.path.dirname(__file__)) + os.sep + "gen-py"))

from thrift.transport.TSocket import TServerSocket
from thrift.protocol.TBinaryProtocol import TBinaryProtocolAcceleratedFactory
from thrift.transport.TTransport import TBufferedTransportFactory
from thrift.server.TProcessPoolServer import TProcessPoolServer
from example.Example import Processor


class Handler(object):
    def add(self, a, b):
        return a + b


if __name__ == "__main__":
    handler = Handler()
    processor = Processor(handler)
    transport = TServerSocket(port=20000)
    tfactory = TBufferedTransportFactory()
    protocol = TBinaryProtocolAcceleratedFactory()

    server = TProcessPoolServer(processor, transport, tfactory, protocol)
    server.setNumWorkers(10)
    server.serve()
Beispiel #11
0
 def __init__(self, host=None, port=9090, certfile='cert.pem', unix_socket=None):
   self.setCertfile(certfile)
   TServerSocket.__init__(self, host, port)