def make_server(host="localhost", port=9090, unix_socket=None, proto_factory=TBinaryProtocolFactory(), trans_factory=TBufferedTransportFactory(), client_timeout=3000, certfile=None): """Creates a Thrift RPC server and serves it with configuration""" processor = create_processor() if unix_socket: server_socket = TServerSocket(unix_socket=unix_socket) if certfile: warnings.warn("SSL only works with host:port, not unix_socket.") elif host and port: if certfile: server_socket = TSSLServerSocket( host=host, port=port, client_timeout=client_timeout, certfile=certfile) else: server_socket = TServerSocket( host=host, port=port, client_timeout=client_timeout) else: raise ValueError("Either host/port or unix_socket must be provided.") server = TThreadedServer(processor, server_socket, iprot_factory=proto_factory, itrans_factory=trans_factory) print('Starting Thrift RPC server running @ %s:%s' % (host, port)) try: server.serve() except KeyboardInterrupt: print() print("Stopping Server from Keyboard Interruption") exit()
def serve(): server_sock = TServerSocket(unix_socket="./thriftpy_test.sock") server_sock.listen() client = server_sock.accept() t = TCyBufferedTransport(client) proto.write_val(t, TType.STRING, val) t.flush()
def serve(): server_sock = TServerSocket(host="127.0.0.1", port=9090) server_sock.listen() client = server_sock.accept() t = TCyBufferedTransport(client) proto.write_val(t, TType.STRING, val) t.flush()
def make_server(service, handler, host="localhost", port=9090, unix_socket=None, proto_factory=TBinaryProtocolFactory(), trans_factory=TBufferedTransportFactory(), client_timeout=3000, certfile=None): processor = TProcessor(service, handler) if unix_socket: server_socket = TServerSocket(unix_socket=unix_socket) if certfile: warnings.warn("SSL only works with host:port, not unix_socket.") elif host and port: if certfile: server_socket = TSSLServerSocket(host=host, port=port, client_timeout=client_timeout, certfile=certfile) else: server_socket = TServerSocket(host=host, port=port, client_timeout=client_timeout) else: raise ValueError("Either host/port or unix_socket must be provided.") server = TThreadedServer(processor, server_socket, iprot_factory=proto_factory, itrans_factory=trans_factory) return server
def serve(): server_sock = TServerSocket( unix_socket="./thriftpy_test.sock") server_sock.listen() client = server_sock.accept() t = TCyBufferedTransport(client) proto.write_val(t, TType.STRING, val) t.flush()
def serve(): server_sock = TServerSocket(unix_socket=unix_sock) server_sock.listen() client = server_sock.accept() t = TCyBufferedTransport(client) proto.write_val(t, TType.STRING, val) t.flush() # wait for client to read time.sleep(1)
def server(request): p1 = TProcessor(mux.ThingOneService, DispatcherOne()) p2 = TProcessor(mux.ThingTwoService, DispatcherTwo()) mux_proc = TMultiplexingProcessor() mux_proc.register_processor(p1) mux_proc.register_processor(p2) _server = TThreadedServer(mux_proc, TServerSocket(unix_socket=sock_path), iprot_factory=TBinaryProtocolFactory(), itrans_factory=TBufferedTransportFactory()) ps = multiprocessing.Process(target=_server.serve) ps.start() time.sleep(0.1) def fin(): if ps.is_alive(): ps.terminate() try: os.remove(sock_path) except IOError: pass request.addfinalizer(fin)
def make_server(service, handler, host, port, proto_factory=TBinaryProtocolFactory()): processor = TProcessor(service, handler) transport = TServerSocket(host=host, port=port) server = TThreadedServer(processor, transport, iprot_factory=proto_factory) return server
def make_server(service, handler, host="localhost", port=9090, unix_socket=None, proto_factory=TBinaryProtocolFactory(), trans_factory=TBufferedTransportFactory()): processor = TProcessor(service, handler) if unix_socket: server_socket = TServerSocket(unix_socket=unix_socket) elif host and port: server_socket = TServerSocket(host=host, port=port) else: raise ValueError("Either host/port or unix_socket must be provided.") server = TThreadedServer(processor, server_socket, iprot_factory=proto_factory, itrans_factory=trans_factory) return server
def _make_server(app, host, port, daemon=True): processor = TProcessor(app.service, app) server_socket = TServerSocket(host=host, port=port) server = TThreadedServer(processor, server_socket, iprot_factory=TBinaryProtocolFactory(), itrans_factory=TBufferedTransportFactory(), daemon=daemon) return server
def create_thrift_server(service, handler, socket_config, server_cls=TThreadedServer): """ 创建Thrift Server对象。 """ return server_cls(TProcessor(service, handler), # processor TServerSocket(**socket_config), # transport TBufferedTransportFactory(), # transportFactory TBinaryProtocolFactory()) # protocolFactory
def init_event_channel(self): """Create a thrift server and register it at ZLP Service to receive events.""" if self._event_channel_handler and not self._event_channel: processor = TProcessor(self.thrift_interface.ClientEventChannel, self._event_channel_handler) server_socket = TServerSocket(host="0.0.0.0", port=0, socket_family=socket.AF_INET, client_timeout=200000) server_socket.client_timeout = 1000 * 60 * 10 self._event_channel = TSimpleServer(processor, server_socket) t = threading.Thread(target=self._event_channel.serve, daemon=True) t.start() time.sleep(1) connection = self._event_channel.trans.sock.getsockname() self.ConnectClientEventChannel(connection[1])
def make_server(host="localhost", port=9090, unix_socket=None, proto_factory=TBinaryProtocolFactory(), trans_factory=TBufferedTransportFactory(), client_timeout=3000, certfile=None): """Creates a Thrift RPC server and serves it with configuration """ _init_django() processor = get_rpc_application() if unix_socket: server_socket = TServerSocket(unix_socket=unix_socket) if certfile: logging.error("SSL only works with host:port, not unix_socket.") elif host and port: if certfile: server_socket = TSSLServerSocket(host=host, port=port, client_timeout=client_timeout, certfile=certfile) else: server_socket = TServerSocket(host=host, port=port, client_timeout=client_timeout) else: raise ValueError("Either host/port or unix_socket must be provided.") server = TThreadedServer(processor, server_socket, iprot_factory=proto_factory, itrans_factory=trans_factory) try: return server except KeyboardInterrupt: print() print("Stopping Server from Keyboard Interruption") exit()
def gen_server(port=16029, tracker=tracker, processor=TTrackedProcessor): args = [processor, addressbook.AddressBookService, Dispatcher()] if tracker: args.insert(1, tracker) processor = TProcessorFactory(*args) server_socket = TServerSocket(host="localhost", port=port) server = TSampleServer(processor, server_socket, prot_factory=TBinaryProtocolFactory(), trans_factory=TBufferedTransportFactory()) ps = multiprocessing.Process(target=server.serve) ps.start() return ps, server
def main(): dd_proc = TProcessor(dd_thrift.DingService, DingDispatcher()) pp_proc = TProcessor(pp_thrift.PingService, PingDispatcher()) mux_proc = TMultiplexedProcessor() mux_proc.register_processor(DD_SERVICE_NAME, dd_proc) mux_proc.register_processor(PP_SERVICE_NAME, pp_proc) server = TThreadedServer(mux_proc, TServerSocket(), iprot_factory=TBinaryProtocolFactory(), itrans_factory=TBufferedTransportFactory()) server.serve()
def make_server(service, handler, fd=None, host=None, port=None, unix_socket=None, address_family=socket.AF_INET, proto_factory=None, trans_factory=None, client_timeout=None, backlog=128): processor = TProcessor(service, handler) if unix_socket is not None: logger.info('Setting up server bound to %s', unix_socket) server_socket = TServerSocket(unix_socket=unix_socket, socket_family=address_family, client_timeout=client_timeout, backlog=backlog) elif fd is not None: logger.info('Setting up server bound to socket fd %s', fd) server_socket = TFDServerSocket(fd=fd, socket_family=address_family, client_timeout=client_timeout, backlog=backlog) elif host is not None and port is not None: logger.info('Setting up server bound to %s:%s', host, str(port)) server_socket = TServerSocket(host=host, port=port, socket_family=address_family, client_timeout=client_timeout, backlog=backlog) else: raise ValueError('Insufficient params') server = TThreadedServer(processor, server_socket, iprot_factory=proto_factory, itrans_factory=trans_factory) return server
def create_multiplexed_server(services, socket_config, server_cls=TThreadedServer): """ 创建多路复用的Thrift Server :param services: 多路复用服务定义,如:[(service1, handler1, service_name1), (service2, handler2, service_name2),...] :param socket_config: Server的socket参数 :param server_cls: 启动的服务器类型 :return: Server对象 """ processor = TMultiplexedProcessor() for service, handler, service_name in services: processor.register_processor(service_name, TProcessor(service, handler)) return server_cls(processor, # processor TServerSocket(**socket_config), # transport TBufferedTransportFactory(), # transportFactory TBinaryProtocolFactory()) # protocolFactory
def make_simple_server(service, handler, host="localhost", port=9090): """Return a server of type TSimple Server. Based on thriftpy's make_server(), but return TSimpleServer instead of TThreadedServer. Since TSimpleServer's constructor doesn't accept kwargs, some arguments of make_server can't be used here. By default: client_timeout: None protocol: TBinaryProtocolFactory transport: TBufferedTransportFactory """ processor = TProcessor(service, handler) if host and port: server_socket = TServerSocket( host=host, port=port, client_timeout=None) else: raise ValueError("Either host/port or unix_socket must be provided.") server = TSimpleServer(processor, server_socket) return server