class ServiceHandler: def __init__(self, host, port): self.config = {'host': host, 'port': port} self.transport = self.set_connection() self.processor = TMultiplexedProcessor() def set_processor(self, item): self.processor.registerProcessor(**item) def set_connection(self): return TSocket.TServerSocket(**self.config) @staticmethod def get_processor(): return [{'serviceName': 'news', 'processor': NewsProcessor}] def run(self): for item in ServiceHandler.get_processor(): self.set_processor(item) print("Serving the Wrapper listener, port: " + str(self.config.get('port'))) return TServer.TThreadedServer( self.processor, self.transport, TTransport.TBufferedTransportFactory(), TBinaryProtocol.TBinaryProtocolFactory())
def run(self): processor = TMultiplexedProcessor() processor.registerProcessor( "DFSService", DistributedFilesystem.Processor( DistributedFileSystemHandler(self.master.dfs) ) ) processor.registerProcessor( "UserService", MasterUserService.Processor( UserServiceHandler(self.master) ) ) server = TServer.TThreadedServer( processor, TSocket.TServerSocket(self.listen_address, self.listen_port), TTransport.TFramedTransportFactory(), TBinaryProtocol.TBinaryProtocolFactory(), daemon=True, ) # Start Tanit distributed filesystem server.serve()
def initTask(self): super(ThriftServerTask, self).initTask() processors = self._findProcessors() assert len(processors) > 0, \ "No processors found for %s" % (self.MODULE) if not self.MULTIPLEX: # For non-multiplexed services, we can only have one processor # that matches the module. assert len(processors) == 1, ( "Too many processors found for %s. Did you mean to set " "MULTIPLEX = True on your server?" % (self.MODULE)) self.processor = processors[0].processor else: # For multiplexed services, register all that match. self.processor = TMultiplexedProcessor() for processor_task in processors: self.logger.info("Registering %s as Multiplexed Service, '%s'", processor_task.processor, processor_task.service_name) self.processor.registerProcessor(processor_task.service_name, processor_task.processor)
def main(args=sys.argv[1:]): from optparse import OptionParser parser = OptionParser() parser.add_option( "-o", "--outdir", dest="outdir", default=tempfile.gettempdir(), help="save trained surrogates or configured optimizers to DIRECTORY", metavar="DIRECTORY", ) (options, args) = parser.parse_args(args) outdir = options.outdir print("Surrogates/Optimizers saved to {}".format(outdir)) processor = TMultiplexedProcessor() processor.registerProcessor( "SurrogateStoreService", SurrogateStoreService.Processor(SurrogateStoreHandler(outdir=outdir)), ) processor.registerProcessor( "OptimizerStoreService", OptimizerStoreService.Processor(OptimizerStoreHandler(outdir=outdir)), ) processor.registerProcessor( "AdministrationService", AdministrationService.Processor(AdministrationHandler()), ) transport = TSocket.TServerSocket("0.0.0.0", port=41400) tfactory = TTransport.TBufferedTransportFactory() pfactory = TBinaryProtocol.TBinaryProtocolFactory() server = TServer.TSimpleServer(processor, transport, tfactory, pfactory) print("Starting WhatsOpt services...") server.serve() print("done!")
def runAccountsHandler(currency_rates, currency_rates_lock, accounts, accounts_lock, port): standard_handler = StandardHandler(accounts, accounts_lock, currency_rates, currency_rates_lock) premium_handler = PremiumHandler(accounts, accounts_lock, currency_rates, currency_rates_lock) processor = TMultiplexedProcessor() processor.registerProcessor("PremiumService", PremiumService.Processor(premium_handler)) processor.registerProcessor("StandardService", StandardService.Processor(standard_handler)) transport = TSocket.TServerSocket(host='127.0.0.1', port=port) server = TServer.TThreadedServer(processor, transport) print('Starting accounts server.') server.serve()
def main(): #handler = Handler() #processor = ocr_server.Processor(handler) handler = Handler() handler1 = Handler1() ocr_processor = ocr_server.Processor(handler) result_processor = result_server.Processor(handler1) processor = TMultiplexedProcessor() processor.registerProcessor("ocr_server", ocr_processor) processor.registerProcessor("result_server", result_processor) addr = "112.74.23.141" port = 6000 print("Server IP: %s, port: %d" % (addr, port)) transport = TSocket.TServerSocket(addr, port=port) tfactory = TTransport.TBufferedTransportFactory() pfactory = TBinaryProtocol.TBinaryProtocolFactory() server = TServer.TSimpleServer(processor, transport, tfactory, pfactory) print "Starting the ocr server." server.serve()
def main(options): # common header allowed client types allowed_client_types = [ THeaderTransport.THeaderClientType.HEADERS, THeaderTransport.THeaderClientType.FRAMED_BINARY, THeaderTransport.THeaderClientType.UNFRAMED_BINARY, THeaderTransport.THeaderClientType.FRAMED_COMPACT, THeaderTransport.THeaderClientType.UNFRAMED_COMPACT, ] # set up the protocol factory form the --protocol option prot_factories = { 'accel': TBinaryProtocol.TBinaryProtocolAcceleratedFactory(), 'multia': TBinaryProtocol.TBinaryProtocolAcceleratedFactory(), 'accelc': TCompactProtocol.TCompactProtocolAcceleratedFactory(), 'multiac': TCompactProtocol.TCompactProtocolAcceleratedFactory(), 'binary': TPedanticSequenceIdProtocolFactory( TBinaryProtocol.TBinaryProtocolFactory()), 'multi': TPedanticSequenceIdProtocolFactory( TBinaryProtocol.TBinaryProtocolFactory()), 'compact': TCompactProtocol.TCompactProtocolFactory(), 'multic': TCompactProtocol.TCompactProtocolFactory(), 'header': THeaderProtocol.THeaderProtocolFactory(allowed_client_types), 'multih': THeaderProtocol.THeaderProtocolFactory(allowed_client_types), 'json': TJSONProtocol.TJSONProtocolFactory(), 'multij': TJSONProtocol.TJSONProtocolFactory(), } pfactory = prot_factories.get(options.proto, None) if pfactory is None: raise AssertionError('Unknown --protocol option: %s' % options.proto) try: pfactory.string_length_limit = options.string_limit pfactory.container_length_limit = options.container_limit except Exception: # Ignore errors for those protocols that does not support length limit pass # get the server type (TSimpleServer, TNonblockingServer, etc...) if len(args) > 1: raise AssertionError( 'Only one server type may be specified, not multiple types.') server_type = args[0] if options.trans == 'http': server_type = 'THttpServer' # Set up the handler and processor objects handler = TestHandler() processor = ThriftTest.Processor(handler) if options.proto.startswith('multi'): secondHandler = SecondHandler() secondProcessor = SecondService.Processor(secondHandler) multiplexedProcessor = TMultiplexedProcessor() multiplexedProcessor.registerDefault(processor) multiplexedProcessor.registerProcessor('ThriftTest', processor) multiplexedProcessor.registerProcessor('SecondService', secondProcessor) processor = multiplexedProcessor global server # Handle THttpServer as a special case if server_type == 'THttpServer': if options.ssl: __certfile = os.path.join(os.path.dirname(SCRIPT_DIR), "keys", "server.crt") __keyfile = os.path.join(os.path.dirname(SCRIPT_DIR), "keys", "server.key") server = THttpServer.THttpServer(processor, ('', options.port), pfactory, cert_file=__certfile, key_file=__keyfile) else: server = THttpServer.THttpServer(processor, ('', options.port), pfactory) server.serve() sys.exit(0) # set up server transport and transport factory abs_key_path = os.path.join(os.path.dirname(SCRIPT_DIR), 'keys', 'server.pem') host = None if options.ssl: from thrift.transport import TSSLSocket transport = TSSLSocket.TSSLServerSocket(host, options.port, certfile=abs_key_path) else: transport = TSocket.TServerSocket(host, options.port, options.domain_socket) tfactory = TTransport.TBufferedTransportFactory() if options.trans == 'buffered': tfactory = TTransport.TBufferedTransportFactory() elif options.trans == 'framed': tfactory = TTransport.TFramedTransportFactory() elif options.trans == '': raise AssertionError('Unknown --transport option: %s' % options.trans) else: tfactory = TTransport.TBufferedTransportFactory() # if --zlib, then wrap server transport, and use a different transport factory if options.zlib: transport = TZlibTransport.TZlibTransport(transport) # wrap with zlib tfactory = TZlibTransport.TZlibTransportFactory() # do server-specific setup here: if server_type == "TNonblockingServer": server = TNonblockingServer.TNonblockingServer( processor, transport, inputProtocolFactory=pfactory) elif server_type == "TProcessPoolServer": import signal from thrift.server import TProcessPoolServer server = TProcessPoolServer.TProcessPoolServer(processor, transport, tfactory, pfactory) server.setNumWorkers(5) def set_alarm(): def clean_shutdown(signum, frame): for worker in server.workers: if options.verbose > 0: logging.info('Terminating worker: %s' % worker) worker.terminate() if options.verbose > 0: logging.info('Requesting server to stop()') try: server.stop() except Exception: pass signal.signal(signal.SIGALRM, clean_shutdown) signal.alarm(4) set_alarm() else: # look up server class dynamically to instantiate server ServerClass = getattr(TServer, server_type) server = ServerClass(processor, transport, tfactory, pfactory) # enter server main loop server.serve()
def __init__(self, host, port): self.config = {'host': host, 'port': port} self.transport = self.set_connection() self.processor = TMultiplexedProcessor()
def main(options): # common header allowed client types allowed_client_types = [ THeaderTransport.THeaderClientType.HEADERS, THeaderTransport.THeaderClientType.FRAMED_BINARY, THeaderTransport.THeaderClientType.UNFRAMED_BINARY, THeaderTransport.THeaderClientType.FRAMED_COMPACT, THeaderTransport.THeaderClientType.UNFRAMED_COMPACT, ] # set up the protocol factory form the --protocol option prot_factories = { 'accel': TBinaryProtocol.TBinaryProtocolAcceleratedFactory(), 'multia': TBinaryProtocol.TBinaryProtocolAcceleratedFactory(), 'accelc': TCompactProtocol.TCompactProtocolAcceleratedFactory(), 'multiac': TCompactProtocol.TCompactProtocolAcceleratedFactory(), 'binary': TPedanticSequenceIdProtocolFactory(TBinaryProtocol.TBinaryProtocolFactory()), 'multi': TPedanticSequenceIdProtocolFactory(TBinaryProtocol.TBinaryProtocolFactory()), 'compact': TCompactProtocol.TCompactProtocolFactory(), 'multic': TCompactProtocol.TCompactProtocolFactory(), 'header': THeaderProtocol.THeaderProtocolFactory(allowed_client_types), 'multih': THeaderProtocol.THeaderProtocolFactory(allowed_client_types), 'json': TJSONProtocol.TJSONProtocolFactory(), 'multij': TJSONProtocol.TJSONProtocolFactory(), } pfactory = prot_factories.get(options.proto, None) if pfactory is None: raise AssertionError('Unknown --protocol option: %s' % options.proto) try: pfactory.string_length_limit = options.string_limit pfactory.container_length_limit = options.container_limit except Exception: # Ignore errors for those protocols that does not support length limit pass # get the server type (TSimpleServer, TNonblockingServer, etc...) if len(args) > 1: raise AssertionError('Only one server type may be specified, not multiple types.') server_type = args[0] if options.trans == 'http': server_type = 'THttpServer' # Set up the handler and processor objects handler = TestHandler() processor = ThriftTest.Processor(handler) if options.proto.startswith('multi'): secondHandler = SecondHandler() secondProcessor = SecondService.Processor(secondHandler) multiplexedProcessor = TMultiplexedProcessor() multiplexedProcessor.registerDefault(processor) multiplexedProcessor.registerProcessor('ThriftTest', processor) multiplexedProcessor.registerProcessor('SecondService', secondProcessor) processor = multiplexedProcessor global server # Handle THttpServer as a special case if server_type == 'THttpServer': if options.ssl: __certfile = os.path.join(os.path.dirname(SCRIPT_DIR), "keys", "server.crt") __keyfile = os.path.join(os.path.dirname(SCRIPT_DIR), "keys", "server.key") server = THttpServer.THttpServer(processor, ('', options.port), pfactory, cert_file=__certfile, key_file=__keyfile) else: server = THttpServer.THttpServer(processor, ('', options.port), pfactory) server.serve() sys.exit(0) # set up server transport and transport factory abs_key_path = os.path.join(os.path.dirname(SCRIPT_DIR), 'keys', 'server.pem') host = None if options.ssl: from thrift.transport import TSSLSocket transport = TSSLSocket.TSSLServerSocket(host, options.port, certfile=abs_key_path) else: transport = TSocket.TServerSocket(host, options.port) tfactory = TTransport.TBufferedTransportFactory() if options.trans == 'buffered': tfactory = TTransport.TBufferedTransportFactory() elif options.trans == 'framed': tfactory = TTransport.TFramedTransportFactory() elif options.trans == '': raise AssertionError('Unknown --transport option: %s' % options.trans) else: tfactory = TTransport.TBufferedTransportFactory() # if --zlib, then wrap server transport, and use a different transport factory if options.zlib: transport = TZlibTransport.TZlibTransport(transport) # wrap with zlib tfactory = TZlibTransport.TZlibTransportFactory() # do server-specific setup here: if server_type == "TNonblockingServer": server = TNonblockingServer.TNonblockingServer(processor, transport, inputProtocolFactory=pfactory) elif server_type == "TProcessPoolServer": import signal from thrift.server import TProcessPoolServer server = TProcessPoolServer.TProcessPoolServer(processor, transport, tfactory, pfactory) server.setNumWorkers(5) def set_alarm(): def clean_shutdown(signum, frame): for worker in server.workers: if options.verbose > 0: logging.info('Terminating worker: %s' % worker) worker.terminate() if options.verbose > 0: logging.info('Requesting server to stop()') try: server.stop() except Exception: pass signal.signal(signal.SIGALRM, clean_shutdown) signal.alarm(4) set_alarm() else: # look up server class dynamically to instantiate server ServerClass = getattr(TServer, server_type) server = ServerClass(processor, transport, tfactory, pfactory) # enter server main loop server.serve()
print("Error Exeption == {}".format(x)) mng_handler = ManageServiceHandler() mng_processor = ManageService.Processor(mng_handler) file_handler = FileServiceHandler() file_processor = FileService.Processor(file_handler) connect_handler = ConnectServiceHandler() conn_processor = ConnectService.Processor(connect_handler) tfactory = TTransport.TBufferedTransportFactory() pfactory = TBinaryProtocol.TBinaryProtocolFactory() processor = TMultiplexedProcessor() #使用TMultiplexedProcessor接收多个处理 processor.registerProcessor("ManageService", mng_processor) processor.registerProcessor("FileService", file_processor) processor.registerProcessor("ConnectService", conn_processor) transport = TSocket.TServerSocket('0.0.0.0', 9999) #server = TServer.TSimpleServer(processor, transport, tfactory, pfactory) server = TServer.TThreadPoolServer(processor, transport, tfactory, pfactory) server.setNumThreads(6000) # 1个主线程 1个监听线程 + 多少个通信线程 # start client connect handler: save client socket info conn_mq_thread = threading.Thread(target=thrift_connect_handler, args=(server, )) conn_mq_thread.start() t = threading.Thread(target=server.serve) #t.setDaemon(self.daemon) t.start()
from thrift.transport import TTransport from thrift.protocol import TBinaryProtocol from thrift.server import TServer from thrift.TMultiplexedProcessor import TMultiplexedProcessor from Config import Config from DemoHandler import DemoHandler if __name__ == "__main__": logging.basicConfig(level=logging.INFO, format='%(asctime)-15s %(message)s', datefmt='%Y-%m-%dT%H:%M:%S') logging.Formatter.converter = time.gmtime processor = TMultiplexedProcessor() processor.registerProcessor("Demo", DemoService.Processor(DemoHandler())) transport = TSocket.TServerSocket('10.1.20.59', port='9090') tfactory = TTransport.TBufferedTransportFactory() pfactory = TBinaryProtocol.TBinaryProtocolFactory() # server = TServer.TSimpleServer(processor, transport, tfactory, pfactory) # You could do one of these for a multithreaded server #server = TServer.TThreadedServer(processor, transport, tfactory, pfactory) #server = TServer.TThreadPoolServer(processor, transport, tfactory, pfactory) server = TServer.TForkingServer(processor, transport, tfactory, pfactory) logging.info('Starting the server...') server.serve()
class ThriftServerTask(VTask): """Base class for various thrift server implementations.""" # If self.MODULE is specified, it means that we specifically want this # server to route requests to a matching ThriftHandler. This is done # so that we can have multiple ThriftServers handling requests for # different services within the same process. MODULE = None MULTIPLEX = False def initTask(self): super(ThriftServerTask, self).initTask() processors = self._findProcessors() assert len(processors) > 0, \ "No processors found for %s" % (self.MODULE) if not self.MULTIPLEX: # For non-multiplexed services, we can only have one processor # that matches the module. assert len(processors) == 1, ( "Too many processors found for %s. Did you mean to set " "MULTIPLEX = True on your server?" % (self.MODULE)) self.processor = processors[0].processor else: # For multiplexed services, register all that match. self.processor = TMultiplexedProcessor() for processor_task in processors: self.logger.info("Registering %s as Multiplexed Service, '%s'", processor_task.processor, processor_task.service_name) self.processor.registerProcessor(processor_task.service_name, processor_task.processor) def _checkTaskModule(self, task): """Returns True if `task` implements the appropriate MODULE Iface""" # Skip non-ThriftHandlerTasks if not isinstance(task, ThriftHandlerTask): return False # If self.MODULE is None, then connect *any* ThriftHandlerTask. if self.MODULE is None: return True # Otherwise, if this is the same thrift module as the handler, we have # a match if self.MODULE is task.MODULE: return True # If they did not match, there's a chance that we used in-process # thrift compilation to produce separate instances of equivalent # modules. Check the individual methods on the handler to make sure # are sort of similar (though the args may differ...). # TODO: Consider re-evaluating this strategy longer-term. iface = self.MODULE.Iface # Verify task has all the Iface methods. for method_name in dir(iface): method = getattr(iface, method_name) # Skip field attributes if not callable(method): continue # Check for this method on the handler task handler_method = getattr(task, method_name, None) if handler_method is None: self.logger.debug("Skipping Task %s (missing method %s)", task.name, method_name) return False # And make sure that attribute is actually callable if not callable(handler_method): self.logger.debug("Skipping Task %s (%s not callable)", task.name, method_name) return False # If all the methods are there, the shoe fits. return True def _findProcessors(self): """Returns all processors that match this tasks' MODULE""" processors = [] for task in self.service.tasks: if self._checkTaskModule(task): processors.append(task) return processors
return json.dumps({token: data}) else: return 'cmd不匹配' class CcktvRoomHandler: def getBannerList(self, dic, idx): print(dic) return RpcResult(idx, 'akatsuki', 'data-mining') if __name__ == "__main__": calculator_processor = Calculator.Processor(TransmitHandler()) ccktv_room_processor = CcktvRoom.Processor(CcktvRoomHandler()) processor = TMultiplexedProcessor() # 接收多个service processor.registerProcessor('calculator', calculator_processor) processor.registerProcessor('ccktv_room', ccktv_room_processor) transport = TSocket.TServerSocket('127.0.0.1', 8000) tfactory = TTransport.TBufferedTransportFactory() pfactory = TBinaryProtocol.TBinaryProtocolFactory() server = TServer.TSimpleServer(processor, transport, tfactory, pfactory) server.serve() # handler = TransmitHandler() # processor = Calculator.Processor(handler) # transport = TSocket.TServerSocket('127.0.0.1', 8000) # tfactory = TTransport.TBufferedTransportFactory() # pfactory = TBinaryProtocol.TBinaryProtocolFactory() # server = TServer.TSimpleServer(processor, transport, tfactory, pfactory)
from thrift.TMultiplexedProcessor import TMultiplexedProcessor multi_processor = TMultiplexedProcessor() class RegisterService(type): def __new__(cls, name, bases, attrs): if 'REGISTER_NAME' not in attrs or not attrs['REGISTER_NAME']: raise Exception('class {} need REGISTER_NAME.'.format(name)) service_name = attrs['REGISTER_NAME'] if 'SERVICE' not in attrs or not attrs['SERVICE']: raise Exception('class {} nedd SERVICE'.format(name)) service = attrs['SERVICE'] handler_cls = super().__new__(cls, name, bases, attrs) processor = service.Processor(handler_cls()) multi_processor.registerProcessor(serviceName=service_name, processor=processor) return handler_cls from handlers import image from handlers import user
#__author__ = 'liuzh' from thrift.TMultiplexedProcessor import TMultiplexedProcessor from thrift.protocol import TBinaryProtocol from thrift.server import TServer from thrift.transport import TSocket, TTransport from com.thrift.test.service import PersonService, FileUploadService from com.thrift.test.service.impl.FileUploadServiceImpl import FileUploadServiceImpl from com.thrift.test.service.impl.PersonServiceImpl import PersonServiceImpl handler = PersonServiceImpl() personProcessor = PersonService.Processor(handler) fileUploadHandler = FileUploadServiceImpl() fileUploadProcessor = FileUploadService.Processor(fileUploadHandler) processor = TMultiplexedProcessor() processor.registerProcessor('PERSON_SERVICE', personProcessor) processor.registerProcessor('FILE_UPLOAD_SERVICE', fileUploadProcessor) transport = TSocket.TServerSocket(port=9988) tfactory = TTransport.TBufferedTransportFactory() pfactory = TBinaryProtocol.TBinaryProtocolFactory() # server = TServer.TSimpleServer(processor, transport, tfactory, pfactory) # You could do one of these for a multithreaded server #server = TServer.TThreadedServer(processor, transport, tfactory, pfactory) server = TServer.TThreadPoolServer(processor, transport, tfactory, pfactory) print('Starting the server...')