def LoadAndServe(host, port): server = SimpleJSONRPCServer((host, port)) for func in API: server.register_function(func.values()[0], func.keys()[0]) server.serve_forever()
def __init__(self, *args, rpc_user, rpc_password, **kargs): self.rpc_user = rpc_user self.rpc_password = rpc_password class VerifyingRequestHandler(SimpleJSONRPCRequestHandler): def parse_request(myself): # first, call the original implementation which returns # True if all OK so far if SimpleJSONRPCRequestHandler.parse_request(myself): # Do not authenticate OPTIONS-requests if myself.command.strip() == 'OPTIONS': return True try: self.authenticate(myself.headers) return True except (RPCAuthCredentialsInvalid, RPCAuthCredentialsMissing, RPCAuthUnsupportedType) as e: myself.send_error(401, str(e)) except BaseException as e: import traceback, sys traceback.print_exc(file=sys.stderr) myself.send_error(500, str(e)) return False SimpleJSONRPCServer.__init__( self, requestHandler=VerifyingRequestHandler, *args, **kargs)
def start_sim_index_server(port, backends=(), remote_urls=(), root=True, logRequests=True): server = SimpleRPCServer(('localhost', port), logRequests=logRequests, requestHandler=RequestHandler) backend_list = list(backends) if remote_urls: backend_list.extend( [RemoteSimIndex(url) for url in remote_urls]) if backend_list: if len(backend_list) == 1: index = ConcurrentSimIndex(backend_list[0]) else: index = ConcurrentSimIndex( SimIndexCollection( shards=backend_list, root=root)) else: index = ConcurrentSimIndex(MemorySimIndex()) index.set_query_scorer('tfidf') server.register_instance(SimIndexService(index)) try: print('Use Control-C to exit') server.serve_forever() except KeyboardInterrupt: print('Exiting')
class ElectrumGui: def __init__(self, config, network): self.network = network self.config = config storage = WalletStorage(self.config.get_wallet_path()) if not storage.file_exists: raise BaseException("Wallet not found") self.wallet = Wallet(storage) self.cmd_runner = Commands(self.config, self.wallet, self.network) host = config.get('rpchost', 'localhost') port = config.get('rpcport', 7777) self.server = SimpleJSONRPCServer((host, port), requestHandler=RequestHandler) self.server.socket.settimeout(1) for cmdname in known_commands: self.server.register_function(getattr(self.cmd_runner, cmdname), cmdname) def main(self, url): self.wallet.start_threads(self.network) while True: try: self.server.handle_request() except socket.timeout: continue except: break self.wallet.stop_threads()
def __init__(self, addr, requestHandler=SimpleJSONRPCRequestHandler, logRequests=True, encoding=None, bind_and_activate=True, address_family=socket.AF_INET, auth_map=None): self.auth_map = auth_map SimpleJSONRPCServer.__init__(self, addr, requestHandler, logRequests, encoding, bind_and_activate, address_family)
def _dispatch(self, method, params): """ Dispatch RVI 'message'. Check if method is 'message', if so dispatch on name 'service_name' instead. """ # print "dispatch:", params if method == 'message': # print "Will dispatch message to: " + params['service_name'] dict_param = {} # Extract the 'parameters' element from the top level JSON-RPC # 'param'. # Convert 'parameters' from [{'vin': 1234}, {hello: 'world'}] to # a regular dictionary: {'vin': 1234, hello: 'world'} #print "Service:", params['service_name'] #print "Parameters:", params['parameters'] msg_params = params['parameters'] for i in range(0, len(msg_params)): for j in range(0, len(msg_params[i].keys())): #print "params", msg_params[i].keys()[j], "=", msg_params[i].values()[j] dict_param[msg_params[i].keys()[j]] = msg_params[i].values()[j] # print "Parameter dictionary: ", dict_param # print # Ship the processed dispatch info upward. return SimpleJSONRPCServer._dispatch(self, params['service_name'], dict_param) return SimpleJSONRPCServer._dispatch(self,message, params)
def _main(args): server = SimpleJSONRPCServer((args.host, args.port)) functions = [f for f in args.functions if f in FUNCTION_TABLE] _message(functions, args.port) for f in functions: server.register_function(FUNCTION_TABLE[f]) server.serve_forever()
def server_thread(conn): from jsonrpclib.SimpleJSONRPCServer import SimpleJSONRPCServer server = SimpleJSONRPCServer(( my_host, my_port)) server.register_function(process_request, 'request') server.register_function(get_mpk, 'mpk') server.register_function(do_stop, 'stop') server.serve_forever()
def __init__(self, name, port, cache_size = 100): self.name = name self.cache = [] self.cache_size = cache_size self.counter = 0 from jsonrpclib.SimpleJSONRPCServer import SimpleJSONRPCServer # https://pypi.python.org/pypi/jsonrpclib/0.1.6 server = SimpleJSONRPCServer(('0.0.0.0', port)) server.register_function(self.count, 'count') server.register_function(self.data, 'data') print >> log.v3, "json-rpc streaming on port", port start_new_thread(server.serve_forever,())
def server_thread(): from jsonrpclib.SimpleJSONRPCServer import SimpleJSONRPCServer host = "127.0.0.1" port = int(serverPort) #http://user:[email protected]:12345 server = SimpleJSONRPCServer((host, port), requestHandler=SleuthSimpleJSONRPCRequestHandler, logRequests=True) server.register_function(handleRequest, 'sendPost') server.serve_forever()
def __init__(self, rvi_node_url, address='localhost', port=(20001,59999)): if isinstance(port, tuple) == True: (start_port, stop_port) = port self.rvi_port = random.randint(start_port, stop_port) else: self.rvi_port = port self.rvi_address = address SimpleJSONRPCServer.__init__(self,addr=((self.rvi_address, self.rvi_port)), logRequests=False) self.rvi_client = Server(rvi_node_url) self.serve_thread = False self.registered_services = dict()
def server_thread(): from SocketServer import ThreadingMixIn from jsonrpclib.SimpleJSONRPCServer import SimpleJSONRPCServer server = SimpleJSONRPCServer(( host, port), requestHandler=authHandler) server.register_function(get_new_address, 'getnewaddress') server.register_function(get_num, 'getnum') server.register_function(get_mpk, 'getkey') server.register_function(do_stop, 'stop') server.serve_forever()
def start_server(): adb = ADB(ADB_PATH) funcs = extract_all_the_functions(adb, 'adb_') server = SimpleJSONRPCServer((RPC_HOST, RPC_PORT)) print type(funcs) for name, func in funcs.iteritems(): server.register_function(func) server.register_function(lambda x: x, 'ping') server.register_function(upload_apk) #server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) server.serve_forever()
def StartRPCServer(self): logging.info('RPC Server: started') rpc_server = SimpleJSONRPCServer((_DEFAULT_BIND_ADDRESS, _GHOST_RPC_PORT), logRequests=False) rpc_server.register_function(self.Reconnect, 'Reconnect') rpc_server.register_function(self.GetStatus, 'GetStatus') rpc_server.register_function(self.RegisterTTY, 'RegisterTTY') rpc_server.register_function(self.RegisterSession, 'RegisterSession') rpc_server.register_function(self.AddToDownloadQueue, 'AddToDownloadQueue') t = threading.Thread(target=rpc_server.serve_forever) t.daemon = True t.start()
def __init__(self): # Setup logging self._setupLogging() # Parse commandline self.parser = argparse.ArgumentParser( description="IPset JSON-RPC daemon", epilog="Config file is reloaded on SIGUSR1", formatter_class=argparse.ArgumentDefaultsHelpFormatter) self.parser.add_argument("--bind", default="127.0.0.1", help="the ip address to bind to") self.parser.add_argument("--port", type=int, default="9090", help="the port to listen on") self.parser.add_argument("--config", default="ipset.conf", help="config file to read ipset mapping from") self.args = self.parser.parse_args() # Init config self.config = configparser.ConfigParser() self._read_config() # Setup config reloading def reload(signal, frame): self._read_config() signal.signal(signal.SIGUSR1, reload) # Init server self.server = SimpleJSONRPCServer((self.args.bind, self.args.port)) # Register handlers self._registerHandlers()
def __init__(self, domain="0.0.0.0", port=5000): self._server = SimpleJSONRPCServer((domain, port)) functions = {"run": self.run} for name in functions.keys(): self._server.register_function(functions[name], name) self._pop = None
def start(self): """This method fires up the daemon server based on initialized parameters of the class""" # initialize the server instance with given resources register_socket('trex_daemon_server') try: print "Firing up TRex REST daemon @ port {trex_port} ...\n".format( trex_port = self.trex_daemon_port ) logger.info("Firing up TRex REST daemon @ port {trex_port} ...".format( trex_port = self.trex_daemon_port )) logger.info("current working dir is: {0}".format(self.TREX_PATH) ) logger.info("current files dir is : {0}".format(self.trex_files_path) ) logger.debug("Starting TRex server. Registering methods to process.") logger.info(self.get_trex_version(base64 = False)) self.server = SimpleJSONRPCServer( (self.trex_host, self.trex_daemon_port) ) except socket.error as e: if e.errno == errno.EADDRINUSE: logger.error("TRex server requested address already in use. Aborting server launching.") print "TRex server requested address already in use. Aborting server launching." raise socket.error(errno.EADDRINUSE, "TRex daemon requested address already in use. " "Server launch aborted. Please make sure no other process is " "using the desired server properties.") elif isinstance(e, socket.gaierror) and e.errno == -3: # handling Temporary failure in name resolution exception raise socket.gaierror(-3, "Temporary failure in name resolution.\n" "Make sure provided hostname has DNS resolving.") else: raise # set further functionality and peripherals to server instance self.server.register_function(self.add) self.server.register_function(self.cancel_reservation) self.server.register_function(self.connectivity_check) self.server.register_function(self.connectivity_check, 'check_connectivity') # alias self.server.register_function(self.force_trex_kill) self.server.register_function(self.get_file) self.server.register_function(self.get_files_list) self.server.register_function(self.get_files_path) self.server.register_function(self.get_running_info) self.server.register_function(self.get_running_status) self.server.register_function(self.get_trex_cmds) self.server.register_function(self.get_trex_daemon_log) self.server.register_function(self.get_trex_log) self.server.register_function(self.get_trex_version) self.server.register_function(self.is_reserved) self.server.register_function(self.is_running) self.server.register_function(self.kill_all_trexes) self.server.register_function(self.push_file) self.server.register_function(self.reserve_trex) self.server.register_function(self.start_trex) self.server.register_function(self.stop_trex) self.server.register_function(self.wait_until_kickoff_finish) signal.signal(signal.SIGTSTP, self.stop_handler) signal.signal(signal.SIGTERM, self.stop_handler) try: self.zmq_monitor.start() self.server.serve_forever() except KeyboardInterrupt: logger.info("Daemon shutdown request detected." ) finally: self.zmq_monitor.join() # close ZMQ monitor thread resources self.server.shutdown()
def main(argv): from jsonrpclib.SimpleJSONRPCServer import SimpleJSONRPCServer gpio = GPIOInterface() gpio.configure_ports(port_numbers) hostname = '127.0.0.1' port = 8001 if '-h' in sys.argv: hostname = sys.argv[sys.argv.index('-h') + 1] if '-p' in sys.argv: port = int(sys.argv[sys.argv.index('-p') + 1]) s = SimpleJSONRPCServer((hostname, port)) s.register_function(gpio.set_port) s.register_function(gpio.get_port) s.serve_forever()
def __init__(self, data=None): self.server = SimpleJSONRPCServer(("localhost", 0), encoding="utf8", requestHandler=JSONRPCRequestHandler) self.rpc_port = self.server.socket.getsockname()[1] self.data = data self.server.register_function(self.get_plottable, "get_plottable") self.server.register_function(self.kill_server, "shutdown") webbrowser.open_new_tab("http://localhost:%d/sliceplot.html" % (self.rpc_port,)) self.server.serve_forever()
def start_master_daemon_func(): funcs_by_name = {} # master_daemon functions funcs_by_name['add'] = add funcs_by_name['check_connectivity'] = check_connectivity funcs_by_name['get_trex_path'] = get_trex_path funcs_by_name['update_trex'] = update_trex # trex_daemon_server funcs_by_name['is_trex_daemon_running'] = trex_daemon_server.is_running funcs_by_name['restart_trex_daemon'] = trex_daemon_server.restart funcs_by_name['start_trex_daemon'] = trex_daemon_server.start funcs_by_name['stop_trex_daemon'] = trex_daemon_server.stop # stl rpc proxy funcs_by_name['is_stl_rpc_proxy_running'] = stl_rpc_proxy.is_running funcs_by_name['restart_stl_rpc_proxy'] = stl_rpc_proxy.restart funcs_by_name['start_stl_rpc_proxy'] = stl_rpc_proxy.start funcs_by_name['stop_stl_rpc_proxy'] = stl_rpc_proxy.stop try: set_logger() register_socket(master_daemon.tag) server = SimpleJSONRPCServer(('0.0.0.0', master_daemon.port)) logging.info('Started master daemon (port %s)' % master_daemon.port) for name, func in funcs_by_name.items(): server.register_function(partial(log_usage, name, func), name) server.register_function(server.funcs.keys, 'get_methods') # should be last signal.signal(signal.SIGTSTP, stop_handler) # ctrl+z signal.signal(signal.SIGTERM, stop_handler) # kill server.serve_forever() except KeyboardInterrupt: logging.info('Ctrl+C') except Exception as e: logging.error('Closing due to error: %s' % e)
def server_thread(conn): from jsonrpclib.SimpleJSONRPCServer import SimpleJSONRPCServer server = SimpleJSONRPCServer((my_host, my_port)) server.register_function(process_request, "request") server.register_function(do_stop, "stop") server.serve_forever()
def main(): """ The code below starts an JSONRPC server """ from jsonrpclib.SimpleJSONRPCServer import SimpleJSONRPCServer parser = optparse.OptionParser(usage="%prog [OPTIONS]") parser.add_option('-p', '--port', default='8080', help='Port to serve on (default 8080)') parser.add_option('-H', '--host', default='127.0.0.1', help='Host to serve on (default localhost; 0.0.0.0 to make public)') parser.add_option('-q', '--quiet', action='store_false', default=True, dest='verbose', help="Quiet mode, don't print status msgs to stdout") parser.add_option('-S', '--corenlp', default=DIRECTORY, help='Stanford CoreNLP tool directory (default %s)' % DIRECTORY) parser.add_option('-P', '--properties', default='default.properties', help='Stanford CoreNLP properties fieles (default: default.properties)') options, args = parser.parse_args() VERBOSE = options.verbose # server = jsonrpc.Server(jsonrpc.JsonRpc20(), # jsonrpc.TransportTcpIp(addr=(options.host, int(options.port)))) try: server = SimpleJSONRPCServer((options.host, int(options.port))) nlp = StanfordCoreNLP(options.corenlp, properties=options.properties, serving=True) server.register_function(nlp.parse) server.register_function(nlp.raw_parse) print 'Serving on http://%s:%s' % (options.host, options.port) # server.serve() server.serve_forever() except KeyboardInterrupt: print >>sys.stderr, "Bye." exit()
def __init__(self, hostname='localhost', port=8080): global methods self._methods = methods self.server = SimpleJSONRPCServer((hostname, port), logRequests=False) self.server.register_introspection_functions() for method in self._methods: self.server.register_function(eval('self.{}'.format(method))) self.processes = {}
class JSONRPCServer(SingletonClass): def __initialize__(self, context_root='/', server_port=8181): SimpleJSONRPCRequestHandler.log_request = log_request SimpleJSONRPCRequestHandler.rpc_paths = (context_root) self.server = SimpleJSONRPCServer(('', server_port)) def registerService(self, serviceName, function): self.server.register_function(function, name=serviceName) def start(self): self.server_proc = Thread(target=self.server.serve_forever) self.server_proc.daemon = True self.server_proc.start() def stop(self): pass
def main(): parser = optparse.OptionParser(usage="%prog [OPTIONS]") parser.add_option('-p', '--port', type="int", default=8080, help="Port to bind to [8080]") parser.add_option('--path', default=DIRECTORY, help="Path to OpenNLP install [%s]" % DIRECTORY) options, args = parser.parse_args() addr = ('localhost', options.port) uri = 'http://%s:%s' % addr server = SimpleJSONRPCServer(addr) print("Starting OpenNLP") nlp = OpenNLP(options.path) server.register_function(nlp.parse) print("Serving on %s" % uri) server.serve_forever()
def _dispatch(self, method, params): if method == 'message': # print "Will dispatch message to: " + params['service_name'] # dict_param = {} # Extract the 'parameters' element from the top level JSON-RPC # 'param'. # Convert 'parameters' from [{'vin': 1234}, {hello: 'world'}] to # a regular dictionary: {'vin': 1234, hello: 'world'} # print "Parameters:", params['parameters'] # print "Parameter disctionary: ", dict_param # print # Ship the processed dispatch info upward. return SimpleJSONRPCServer._dispatch(self, params['service_name'], params['parameters']) # Fallthrough to all other methods. # Will handle service_re3 return SimpleJSONRPCServer._dispatch(self,method, params)
class BackEndPool(Daemon): def __init__(self): self.backends = {} def run(self): self.server = SimpleJSONRPCServer(('localhost', 8473)) self.server.register_function(self.spawn_backends, "spawn") self.server.register_function(self.kill_backends, "kill") self.server.serve_forever() def spawn_backends(self, backend): space = backend['space'] model = backend['model'] if space not in self.backends: self.backends[space] = {} if model not in self.backends[space]: self.backends[space][model] = [] self.backends[space][model].append(BackEndProcess(backend)) for be in self.backends[backend['space']][backend['model']]: be.start() def kill_backends(self, backend): for be in self.backends[backend['space']][backend['model']]: be.join()
class GDFANN_Node: def __init__(self, domain='0.0.0.0', port=5000): self._server = SimpleJSONRPCServer( (domain, port) ) functions = { 'run' : self.run } for name in functions.keys(): self._server.register_function(functions[name], name) self._pop = None def run(self, population): if type(population) <> list: return 'Population must be a list' if self._pop is not None: del(self._pop) self._pop = Populacao(None, verboso=True) for i in population: print i self._pop.addIndividuo(Cromossomo(9,i)) print 'Evaluating population...' self._pop.avaliarPopulacao(avaliacaoRNA) print 'Sending answers...' answersMSE = [] for i in self._pop.getIndividuos(): answersMSE.append(i.getAvaliacao()) print answersMSE return answersMSE def serve(self): try: print 'Server running. Press CTRL+C to stop...' self._server.serve_forever() except KeyboardInterrupt: print 'Exiting'
def main(): global _PIDS # Create a new JSON-RPC server on localhost:8080 s = SimpleJSONRPCServer(('localhost', 8080)) # Register the functions to be called by the PHP client s.register_function(parse_html, 'parse_html') # Creates 5 child server processes for i in range(5): # Fork current process pid = os.fork() # Child fork: if 0 == pid: # Prevent interrupt messages for signum in ( signal.SIGINT, signal.SIGTERM, ): signal.signal(signum, _gogentle) # Start server s.serve_forever() os._exit(0) # Parent: else: _PIDS.append(pid) # Handle interrupt signals quietly for signum in ( signal.SIGINT, signal.SIGTERM, ): signal.signal(signum, _kronos) # Wait for child processes while len(_PIDS): pid, rc = os.waitpid(-1, 0) _PIDS.remove(pid) return 0
def server_set_up(addr, address_family=socket.AF_INET): # Not sure this is a good idea to spin up a new server thread # for each test... but it seems to work fine. def log_request(self, *args, **kwargs): """ Making the server output 'quiet' """ pass SimpleJSONRPCRequestHandler.log_request = log_request server = SimpleJSONRPCServer(addr, address_family=address_family) service = ExampleAggregateService() # Expose an instance of the service server.register_instance(service, allow_dotted_names=True) # Expose some aliases for service methods server.register_function(service.summation, 'sum') server.register_function(service.summation, 'notify_sum') server.register_function(service.summation, 'namespace.sum') server_proc = Thread(target=server.serve_forever) server_proc.daemon = True server_proc.start() return server_proc
class Daemon(DaemonThread): def __init__(self, config, network): DaemonThread.__init__(self) self.config = config self.network = network self.wallets = {} self.load_wallet() self.cmd_runner = Commands(self.config, self.wallets, self.network) host = config.get('rpchost', '0.0.0.0') port = config.get('rpcport', 8000) self.server = SimpleJSONRPCServer((host, port), requestHandler=RequestHandler, logRequests=False) with open(lockfile(config), 'w') as f: f.write(repr(self.server.socket.getsockname())) self.server.timeout = 0.1 for cmdname in known_commands: # rpc直接调用命令 --hetao self.server.register_function(getattr(self.cmd_runner, cmdname), cmdname) self.server.register_function(self.ping, 'ping') self.server.register_function(self.run_daemon, 'daemon') def ping(self): return True def run_daemon(self, config): sub = config.get('subcommand') assert sub in ['start', 'stop', 'status'] if sub == 'start': response = "Daemon already running" elif sub == 'status': p = self.network.get_parameters() response = { 'path': self.network.config.path, 'server': p[0], 'blockchain_height': self.network.get_local_height(), 'server_height': self.network.get_server_height(), 'nodes': self.network.get_interfaces(), 'connected': self.network.is_connected(), 'auto_connect': p[3], 'wallets': dict([(k, w.is_up_to_date()) for k, w in self.wallets.items()]), } elif sub == 'stop': self.stop() response = "Daemon stopped" return response def load_wallet(self): mongo = pymongo.MongoClient('192.168.14.240') db = mongo.uwallet_user for col_name in db.list_collection_names(): col = db.get_collection(col_name) for user_name in col.find({}, {'_id': 1}): user = '******'.join([col_name, user_name['_id']]) storage = WalletStorage(user) wallet = Wallet(storage) # automatically generate wallet for ulord if not storage.file_exists: seed = wallet.make_seed() wallet.add_seed(seed, None) wallet.create_master_keys(None) wallet.create_main_account() wallet.synchronize() wallet.start_threads(self.network) if wallet: self.wallets[user] = wallet def run(self): i = 0 while self.is_running(): # self.server.handle_request() try: thread.start_new_thread(self.server.handle_request, ()) time.sleep(0.01) i = i + 1 except Exception, ex: i = 0 print ex continue os.unlink(lockfile(self.config))
time.sleep(MODEL_UPDATE_LAG_IN_SECONDS) restoreVars() loadModel() # Setup watchdog observer = Observer() observer.schedule(ReloadModelHandler(), path=MODEL_DIR, recursive=False) observer.start() def classify(text): text_series = pd.Series([text]) predict_x = np.array(list(vocab_processor.transform(text_series))) print(predict_x) y_predicted = [ p['class'] for p in classifier.predict(predict_x, as_iterable=True) ] print(y_predicted[0]) topic = news_classes.class_map[str(y_predicted[0])] return topic # Threading RPC server RPC_SERVER = SimpleJSONRPCServer((SERVER_HOST, SERVER_PORT)) RPC_SERVER.register_function(classify, 'classify') print("Starting RPC Server on %s:%d" % (SERVER_HOST, SERVER_PORT)) RPC_SERVER.serve_forever()
class CTRexServer(object): """This class defines the server side of the RESTfull interaction with TRex""" DEFAULT_TREX_PATH = '/auto/proj-pcube-b/apps/PL-b/tools/bp_sim2/v1.55/' #'/auto/proj-pcube-b/apps/PL-b/tools/nightly/trex_latest' TREX_START_CMD = './t-rex-64' DEFAULT_FILE_PATH = '/tmp/trex_files/' def __init__(self, trex_path, trex_files_path, trex_host='0.0.0.0', trex_daemon_port=8090, trex_zmq_port=4500): """ Parameters ---------- trex_host : str a string of the t-rex ip address or hostname. default value: machine hostname as fetched from socket.gethostname() trex_daemon_port : int the port number on which the trex-daemon server can be reached default value: 8090 trex_zmq_port : int the port number on which trex's zmq module will interact with daemon server default value: 4500 Instantiate a TRex client object, and connecting it to listening daemon-server """ self.TREX_PATH = os.path.abspath(os.path.dirname(trex_path + '/')) self.trex_files_path = os.path.abspath( os.path.dirname(trex_files_path + '/')) self.__check_trex_path_validity() self.__check_files_path_validity() self.trex = CTRex() self.trex_version = None self.trex_host = trex_host self.trex_daemon_port = trex_daemon_port self.trex_zmq_port = trex_zmq_port self.trex_server_path = "http://{hostname}:{port}".format( hostname=trex_host, port=trex_daemon_port) self.start_lock = threading.Lock() self.__reservation = None self.zmq_monitor = ZmqMonitorSession( self.trex, self.trex_zmq_port ) # intiate single ZMQ monitor thread for server usage def add(self, x, y): print("server function add ", x, y) logger.info( "Processing add function. Parameters are: {0}, {1} ".format(x, y)) return x + y # return Fault(-10, "") def push_file(self, filename, bin_data): logger.info("Processing push_file() command.") try: filepath = os.path.abspath( os.path.join(self.trex_files_path, filename)) with open(filepath, 'wb') as f: f.write(binascii.a2b_base64(bin_data)) logger.info( "push_file() command finished. `{name}` was saved at {fpath}". format(name=filename, fpath=self.trex_files_path)) return True except IOError as inst: logger.error("push_file method failed. " + str(inst)) return False def connectivity_check(self): logger.info("Processing connectivity_check function.") return True def start(self): """This method fires up the daemon server based on initialized parameters of the class""" # initialize the server instance with given resources try: print("Firing up TRex REST daemon @ port {trex_port} ...\n".format( trex_port=self.trex_daemon_port)) logger.info( "Firing up TRex REST daemon @ port {trex_port} ...".format( trex_port=self.trex_daemon_port)) logger.info("current working dir is: {0}".format(self.TREX_PATH)) logger.info("current files dir is : {0}".format( self.trex_files_path)) logger.debug( "Starting TRex server. Registering methods to process.") logger.info(self.get_trex_version(base64=False)) self.server = SimpleJSONRPCServer( (self.trex_host, self.trex_daemon_port)) except socket.error as e: if e.errno == errno.EADDRINUSE: logger.error( "TRex server requested address already in use. Aborting server launching." ) print( "TRex server requested address already in use. Aborting server launching." ) raise socket.error( errno.EADDRINUSE, "TRex daemon requested address already in use. " "Server launch aborted. Please make sure no other process is " "using the desired server properties.") elif isinstance(e, socket.gaierror) and e.errno == -3: # handling Temporary failure in name resolution exception raise socket.gaierror( -3, "Temporary failure in name resolution.\n" "Make sure provided hostname has DNS resolving.") else: raise # set further functionality and peripherals to server instance try: self.server.register_function(self.add) self.server.register_function(self.get_trex_log) self.server.register_function(self.get_trex_daemon_log) self.server.register_function(self.get_trex_version) self.server.register_function(self.connectivity_check) self.server.register_function(self.start_trex) self.server.register_function(self.stop_trex) self.server.register_function(self.wait_until_kickoff_finish) self.server.register_function(self.get_running_status) self.server.register_function(self.is_running) self.server.register_function(self.get_running_info) self.server.register_function(self.is_reserved) self.server.register_function(self.get_files_path) self.server.register_function(self.push_file) self.server.register_function(self.reserve_trex) self.server.register_function(self.cancel_reservation) self.server.register_function(self.force_trex_kill) signal.signal(signal.SIGTSTP, self.stop_handler) signal.signal(signal.SIGTERM, self.stop_handler) self.zmq_monitor.start() self.server.serve_forever() except KeyboardInterrupt: logger.info("Daemon shutdown request detected.") finally: self.zmq_monitor.join() # close ZMQ monitor thread resources self.server.shutdown() pass # get files from Trex server and return their content (mainly for logs) @staticmethod def _pull_file(filepath): try: with open(filepath, 'rb') as f: file_content = f.read() return binascii.b2a_base64(file_content) except Exception as e: err_str = "Can't get requested file: {0}, possibly due to TRex that did not run".format( filepath) logger.error('{0}, error: {1}'.format(err_str, e)) return Fault(-33, err_str) # get Trex log /tmp/trex.txt def get_trex_log(self): logger.info("Processing get_trex_log() command.") return self._pull_file('/tmp/trex.txt') # get daemon log /var/log/trex/trex_daemon_server.log def get_trex_daemon_log(self): logger.info("Processing get_trex_daemon_log() command.") return self._pull_file('/var/log/trex/trex_daemon_server.log') # get Trex version from ./t-rex-64 --help (last lines starting with "Version : ...") def get_trex_version(self, base64=True): try: logger.info("Processing get_trex_version() command.") if not self.trex_version: help_print = subprocess.Popen(['./t-rex-64', '--help'], cwd=self.TREX_PATH, stdout=subprocess.PIPE, stderr=subprocess.PIPE) (stdout, stderr) = help_print.communicate() search_result = re.search('\n\s*(Version\s*:.+)', stdout, re.DOTALL) if not search_result: raise Exception( 'Could not determine version from ./t-rex-64 --help') self.trex_version = binascii.b2a_base64(search_result.group(1)) if base64: return self.trex_version else: return binascii.a2b_base64(self.trex_version) except Exception as e: err_str = "Can't get trex version, error: {0}".format(e) logger.error(err_str) return Fault(-33, err_str) def stop_handler(self, signum, frame): logger.info("Daemon STOP request detected.") if self.is_running(): # in case TRex process is currently running, stop it before terminating server process self.stop_trex(self.trex.get_seq()) sys.exit(0) def is_running(self): run_status = self.trex.get_status() logger.info( "Processing is_running() command. Running status is: {stat}". format(stat=run_status)) if run_status == TRexStatus.Running: return True else: return False def is_reserved(self): logger.info("Processing is_reserved() command.") return bool(self.__reservation) def get_running_status(self): run_status = self.trex.get_status() logger.info( "Processing get_running_status() command. Running status is: {stat}" .format(stat=run_status)) return { 'state': run_status.value, 'verbose': self.trex.get_verbose_status() } def get_files_path(self): logger.info("Processing get_files_path() command.") return self.trex_files_path def reserve_trex(self, user): if user == "": logger.info( "TRex reservation cannot apply to empty string user. Request denied." ) return Fault( -33, "TRex reservation cannot apply to empty string user. Request denied." ) with self.start_lock: logger.info("Processing reserve_trex() command.") if self.is_reserved(): if user == self.__reservation['user']: # return True is the same user is asking and already has the resrvation logger.info( "the same user is asking and already has the resrvation. Re-reserving TRex." ) return True logger.info( "TRex is already reserved to another user ({res_user}), cannot reserve to another user." .format(res_user=self.__reservation['user'])) return Fault( -33, "TRex is already reserved to another user ({res_user}). Please make sure TRex is free before reserving it." .format(res_user=self.__reservation['user'] )) # raise at client TRexInUseError elif self.trex.get_status() != TRexStatus.Idle: logger.info( "TRex is currently running, cannot reserve TRex unless in Idle state." ) return Fault( -13, 'TRex is currently running, cannot reserve TRex unless in Idle state. Please try again when TRex run finished.' ) # raise at client TRexInUseError else: logger.info( "TRex is now reserved for user ({res_user}).".format( res_user=user)) self.__reservation = {'user': user, 'since': time.ctime()} logger.debug("Reservation details: " + str(self.__reservation)) return True def cancel_reservation(self, user): with self.start_lock: logger.info("Processing cancel_reservation() command.") if self.is_reserved(): if self.__reservation['user'] == user: logger.info( "TRex reservation to {res_user} has been canceled successfully." .format(res_user=self.__reservation['user'])) self.__reservation = None return True else: logger.warning( "TRex is reserved to different user than the provided one. Reservation wasn't canceled." ) return Fault( -33, "Cancel reservation request is available to the user that holds the reservation. Request denied" ) # raise at client TRexRequestDenied else: logger.info( "TRex is not reserved to anyone. No need to cancel anything" ) assert (self.__reservation is None) return False def start_trex(self, trex_cmd_options, user, block_to_success=True, timeout=40): with self.start_lock: logger.info("Processing start_trex() command.") if self.is_reserved(): # check if this is not the user to which TRex is reserved if self.__reservation['user'] != user: logger.info( "TRex is reserved to another user ({res_user}). Only that user is allowed to initiate new runs." .format(res_user=self.__reservation['user'])) return Fault( -33, "TRex is reserved to another user ({res_user}). Only that user is allowed to initiate new runs." .format(res_user=self.__reservation['user'] )) # raise at client TRexRequestDenied elif self.trex.get_status() != TRexStatus.Idle: logger.info( "TRex is already taken, cannot create another run until done." ) return Fault(-13, '') # raise at client TRexInUseError try: server_cmd_data = self.generate_run_cmd(**trex_cmd_options) self.zmq_monitor.first_dump = True self.trex.start_trex(self.TREX_PATH, server_cmd_data) logger.info("TRex session has been successfully initiated.") if block_to_success: # delay server response until TRex is at 'Running' state. start_time = time.time() trex_state = None while (time.time() - start_time) < timeout: trex_state = self.trex.get_status() if trex_state != TRexStatus.Starting: break else: time.sleep(0.5) # check for TRex run started normally if trex_state == TRexStatus.Starting: # reached timeout logger.warning( "TimeoutError: TRex initiation outcome could not be obtained, since TRex stays at Starting state beyond defined timeout." ) return Fault( -12, 'TimeoutError: TRex initiation outcome could not be obtained, since TRex stays at Starting state beyond defined timeout.' ) # raise at client TRexWarning elif trex_state == TRexStatus.Idle: return Fault(-11, self.trex.get_verbose_status() ) # raise at client TRexError # reach here only if TRex is at 'Running' state self.trex.gen_seq() return self.trex.get_seq( ) # return unique seq number to client except TypeError as e: logger.error( "TRex command generation failed, probably because either -f (traffic generation .yaml file) and -c (num of cores) was not specified correctly.\nReceived params: {params}" .format(params=trex_cmd_options)) raise TypeError( 'TRex -f (traffic generation .yaml file) and -c (num of cores) must be specified.' ) def stop_trex(self, seq): logger.info("Processing stop_trex() command.") if self.trex.get_seq() == seq: logger.debug("Abort request legit since seq# match") return self.trex.stop_trex() else: if self.trex.get_status() != TRexStatus.Idle: logger.warning( "Abort request is only allowed to process initiated the run. Request denied." ) return Fault( -33, 'Abort request is only allowed to process initiated the run. Request denied.' ) # raise at client TRexRequestDenied else: return False def force_trex_kill(self): logger.info( "Processing force_trex_kill() command. --> Killing TRex session indiscriminately." ) return self.trex.stop_trex() def wait_until_kickoff_finish(self, timeout=40): # block until TRex exits Starting state logger.info("Processing wait_until_kickoff_finish() command.") trex_state = None start_time = time.time() while (time.time() - start_time) < timeout: trex_state = self.trex.get_status() if trex_state != TRexStatus.Starting: return return Fault( -12, 'TimeoutError: TRex initiation outcome could not be obtained, since TRex stays at Starting state beyond defined timeout.' ) # raise at client TRexWarning def get_running_info(self): logger.info("Processing get_running_info() command.") return self.trex.get_running_info() def generate_run_cmd(self, f, d, iom=0, export_path="/tmp/trex.txt", **kwargs): """ generate_run_cmd(self, trex_cmd_options, export_path) -> str Generates a custom running command for the kick-off of the TRex traffic generator. Returns a tuple of command (string) and export path (string) to be issued on the trex server Parameters ---------- kwargs: dictionary Dictionary of parameters for trex. For example: (c=1, nc=True, l_pkt_mode=3). Notice that when sending command line parameters that has -, you need to replace it with _. for example, to have on command line "--l-pkt-mode 3", you need to send l_pkt_mode=3 export_path : str Full system path to which the results of the trex-run will be logged. """ if 'results_file_path' in kwargs: export_path = kwargs['results_file_path'] del kwargs['results_file_path'] # adding additional options to the command trex_cmd_options = '' for key, value in kwargs.items(): tmp_key = key.replace('_', '-') dash = ' -' if (len(key) == 1) else ' --' if (value == True) and ( str(value) != '1' ): # checking also int(value) to excape from situation that 1 translates by python to 'True' trex_cmd_options += (dash + tmp_key) else: trex_cmd_options += (dash + '{k} {val}'.format(k=tmp_key, val=value)) cmd = "{run_command} -f {gen_file} -d {duration} --iom {io} {cmd_options} --no-key > {export}".format( # -- iom 0 disables the periodic log to the screen (not needed) run_command=self.TREX_START_CMD, gen_file=f, duration=d, cmd_options=trex_cmd_options, io=iom, export=export_path) logger.info("TREX FULL COMMAND: {command}".format(command=cmd)) return (cmd, export_path, int(d)) def __check_trex_path_validity(self): # check for executable existance if not os.path.exists(self.TREX_PATH + '/t-rex-64'): print( "The provided TRex path do not contain an executable TRex file.\nPlease check the path and retry." ) logger.error( "The provided TRex path do not contain an executable TRex file" ) exit(-1) # check for executable permissions st = os.stat(self.TREX_PATH + '/t-rex-64') if not bool(st.st_mode & (stat.S_IXUSR)): print( "The provided TRex path do not contain an TRex file with execution privileges.\nPlease check the files permissions and retry." ) logger.error( "The provided TRex path do not contain an TRex file with execution privileges" ) exit(-1) else: return def __check_files_path_validity(self): # first, check for path existance. otherwise, try creating it with appropriate credentials if not os.path.exists(self.trex_files_path): try: os.makedirs(self.trex_files_path, 0o660) return except os.error as inst: print( "The provided files path does not exist and cannot be created with needed access credentials using root user.\nPlease check the path's permissions and retry." ) logger.error( "The provided files path does not exist and cannot be created with needed access credentials using root user." ) exit(-1) elif os.access(self.trex_files_path, os.W_OK): return else: print( "The provided files path has insufficient access credentials for root user.\nPlease check the path's permissions and retry." ) logger.error( "The provided files path has insufficient access credentials for root user" ) exit(-1)
def daemon(self, config): network = self.network devices = self.devices workers = {} def _classify(params): ret = { } output_dim = {} hash = hashlib.new('ripemd160') hash.update(json.dumps(params)) hash = hash.hexdigest() for k in params: try: params[k] = numpy.asarray(params[k], dtype='float32') if k != 'data': output_dim[k] = network.n_out[k] # = [network.n_in,2] if k == 'data' else network.n_out[k] except Exception: if k != 'data' and not k in network.n_out: ret['error'] = 'unknown target: %s' % k else: ret['error'] = 'unable to convert %s to an array from value %s' % (k,str(params[k])) break if not 'error' in ret: data = StaticDataset(data=[params], output_dim=output_dim) data.init_seq_order() try: data = StaticDataset(data=[params], output_dim=output_dim) data.init_seq_order() except Exception: ret['error'] = "invalid data: %s" % params else: batches = data.generate_batches(recurrent_net=network.recurrent, batch_size=sys.maxsize, max_seqs=1) if not hash in workers: workers[hash] = ClassificationTaskThread(network, devices, data, batches) workers[hash].json_params = params print("worker started:", hash, file=log.v3) ret['result'] = { 'hash' : hash } return ret def _backprob(params): ret = {} def _result(hash): if not workers[hash].isAlive(): return { 'result' : { k : workers[hash].result[k].tolist() for k in workers[hash].result } } else: return { 'error' : "working ..."} class RequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler): def do_POST(self): if len(self.path) == 0: self.send_response(404) return self.path = self.path[1:] ret = {} if self.path in ['classify']: ctype, pdict = cgi.parse_header(self.headers.getheader('content-type')) if ctype == 'application/json': length = int(self.headers.getheader('content-length')) params = cgi.parse_qs(self.rfile.read(length),keep_blank_values=1) try: content = params.keys()[0].decode('utf-8') # this is weird params = json.loads(content) except Exception: ret['error'] = 'unable to decode object' else: ret.update(_classify(params)) else: ret['error'] = 'invalid header: %s' % ctype else: ret['error'] = 'invalid command: %s' % self.path self.send_response(200) self.send_header('Content-Type', 'application/json') self.wfile.write("\n") self.wfile.write(json.dumps(ret)) self.end_headers() def do_GET(self): if len(self.path.replace('/', '')) == 0: self.send_response(200) else: if len(self.path) == 0: self.send_response(404) return ret = { 'error' : "" } self.path = self.path[1:].split('/') if self.path[0] in ['result']: if self.path[1] in workers: if not workers[self.path[1]].isAlive(): ret['result'] = { k : workers[self.path[1]].result[k] for k in workers[self.path[1]].result } else: ret['error'] = "working ..." else: ret['error'] = "unknown hash: " % self.path[1] else: ret['error'] = "invalid command: %s" % self.path[0] self.send_response(200) self.send_header('Content-Type', 'application/json') self.wfile.write("\n") self.wfile.write(json.dumps(ret)) self.end_headers() def log_message(self, format, *args): pass class ThreadingServer(SocketServer.ThreadingMixIn, BaseHTTPServer.HTTPServer): pass port = config.int('daemon.port', 3333) httpd = ThreadingServer(("", port), RequestHandler) print("httpd listening on port", port, file=log.v3) try: from jsonrpclib.SimpleJSONRPCServer import SimpleJSONRPCServer # https://pypi.python.org/pypi/jsonrpclib/0.1.6 except Exception: httpd.serve_forever() else: from thread import start_new_thread start_new_thread(httpd.serve_forever, ()) server = SimpleJSONRPCServer(('0.0.0.0', port+1)) server.register_function(_classify, 'classify') server.register_function(_result, 'result') server.register_function(_backprob, 'backprob') print("json-rpc listening on port", port+1, file=log.v3) server.serve_forever()
def main(): server = SimpleJSONRPCServer(('localhost', 1006)) server.register_function(predict) print("Start server") server.serve_forever()
def __init__(self, *args, **kwargs): SimpleJSONRPCServer.__init__(self, *args, **kwargs)
import sys from bson.json_util import dumps from jsonrpclib.SimpleJSONRPCServer import SimpleJSONRPCServer # import utils packages sys.path.append(os.path.join(os.path.dirname(__file__), 'utils')) import mongodb_client # pylint: disable=import-error, wrong-import-position SERVER_HOST = 'localhost' SERVER_PORT = 4040 def add(num1, num2): """Test method""" print("Add is called with %d and %d" % (num1, num2)) return num1 + num2 def get_one_news(): """Get One News""" print("get_one_news is called") news = mongodb_client.get_db()['news'].find_one() return json.loads(dumps(news)) RPC_SERVER = SimpleJSONRPCServer((SERVER_HOST, SERVER_PORT)) RPC_SERVER.register_function(add, 'add') RPC_SERVER.register_function(get_one_news, 'getOneNews') print("Starting RPC Server on %s:%d " % (SERVER_HOST, SERVER_PORT)) RPC_SERVER.serve_forever()
class OverlordClientDaemon(object): """Overlord Client Daemon.""" def __init__(self): self._state = DaemonState() self._server = None def Start(self): self.StartRPCServer() def StartRPCServer(self): self._server = SimpleJSONRPCServer(_OVERLORD_CLIENT_DAEMON_RPC_ADDR, logRequests=False) exports = [ (self.State, 'State'), (self.Ping, 'Ping'), (self.GetPid, 'GetPid'), (self.Connect, 'Connect'), (self.Clients, 'Clients'), (self.SelectClient, 'SelectClient'), (self.AddForward, 'AddForward'), (self.RemoveForward, 'RemoveForward'), (self.RemoveAllForward, 'RemoveAllForward'), ] for func, name in exports: self._server.register_function(func, name) pid = os.fork() if pid == 0: self._server.serve_forever() @staticmethod def GetRPCServer(): """Returns the Overlord client daemon RPC server.""" server = jsonrpclib.Server('http://%s:%d' % _OVERLORD_CLIENT_DAEMON_RPC_ADDR) try: server.Ping() except Exception: return None return server def State(self): return self._state def Ping(self): return True def GetPid(self): return os.getpid() def _GetJSON(self, path): url = '%s:%d%s' % (self._state.host, self._state.port, path) return json.loads(UrlOpen(self._state, url).read()) def _TLSEnabled(self): """Determine if TLS is enabled on given server address.""" sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: # Allow any certificate since we only want to check if server talks TLS. context = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2) context.verify_mode = ssl.CERT_NONE sock = context.wrap_socket(sock, server_hostname=self._state.host) sock.settimeout(_CONNECT_TIMEOUT) sock.connect((self._state.host, self._state.port)) return True except ssl.SSLError as e: return False except socket.error: # Connect refused or timeout raise except Exception: return False # For whatever reason above failed, assume False def _CheckTLSCertificate(self): """Check TLS certificate. Returns: A tupple (check_result, if_certificate_is_loaded) """ def _DoConnect(context): sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: sock.settimeout(_CONNECT_TIMEOUT) sock = context.wrap_socket(sock, server_hostname=self._state.host) sock.connect((self._state.host, self._state.port)) except ssl.SSLError: return False finally: sock.close() # Save SSLContext for future use. self._state.ssl_context = context return True # First try connect with built-in certificates tls_context = ssl.create_default_context(ssl.Purpose.SERVER_AUTH) if _DoConnect(tls_context): return True # Try with already saved certificate, if any. tls_context = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2) tls_context.verify_mode = ssl.CERT_REQUIRED tls_context.check_hostname = True tls_cert_path = GetTLSCertPath(self._state.host) if os.path.exists(tls_cert_path): tls_context.load_verify_locations(tls_cert_path) self._state.ssl_self_signed = True return _DoConnect(tls_context) def Connect(self, host, port=_OVERLORD_HTTP_PORT, ssh_pid=None, username=None, password=None, orig_host=None): self._state.username = username self._state.password = password self._state.host = host self._state.port = port self._state.ssl = False self._state.ssl_self_signed = False self._state.orig_host = orig_host self._state.ssh_pid = ssh_pid self._state.selected_mid = None tls_enabled = self._TLSEnabled() if tls_enabled: result = self._CheckTLSCertificate() if not result: if self._state.ssl_self_signed: return ('SSLCertificateChanged', ssl.get_server_certificate( (self._state.host, self._state.port))) else: return ('SSLVerifyFailed', ssl.get_server_certificate( (self._state.host, self._state.port))) try: self._state.ssl = tls_enabled UrlOpen(self._state, '%s:%d' % (host, port)) except urllib2.HTTPError as e: return ('HTTPError', e.getcode(), str(e), e.read().strip()) except Exception as e: return str(e) else: return True def Clients(self): if time.time() - self._state.last_list <= _LIST_CACHE_TIMEOUT: return self._state.listing mids = [client['mid'] for client in self._GetJSON('/api/agents/list')] self._state.listing = sorted(list(set(mids))) self._state.last_list = time.time() return self._state.listing def SelectClient(self, mid): self._state.selected_mid = mid def AddForward(self, mid, remote, local, pid): self._state.forwards[local] = (mid, remote, pid) def RemoveForward(self, local_port): try: unused_mid, unused_remote, pid = self._state.forwards[local_port] KillGraceful(pid) del self._state.forwards[local_port] except (KeyError, OSError): pass def RemoveAllForward(self): for unused_mid, unused_remote, pid in self._state.forwards.values(): try: KillGraceful(pid) except OSError: pass self._state.forwards = {}
def startservers(self): server = SimpleJSONRPCServer((IP, self.serverport)) server.register_function(self.deviceConnected) server.register_function(self.deviceDisconnected) server.register_function(self.portAdded) server.register_function(self.portRemoved) server.register_function(self.linkup) server.register_function(self.linkdown) try: self.fvthread = threading.Thread(target=server.serve_forever) print "Start callback server" self.fvthread.start() self.httpd.serve_forever() except KeyboardInterrupt: server.shutdown() self.unregisterFromFV()
sys.exit(1) # create watching_only wallet storage = electrum.WalletStorage(c) if not storage.file_exists: print "creating wallet file" wallet = electrum.wallet.Wallet.from_xpub(xpub, storage) else: wallet = electrum.wallet.Wallet(storage) wallet.synchronize = lambda: None # prevent address creation by the wallet wallet.start_threads(network) network.register_callback('updated', on_wallet_update) threading.Thread(target=db_thread, args=()).start() out_queue = Queue.Queue() # server thread from jsonrpclib.SimpleJSONRPCServer import SimpleJSONRPCServer server = SimpleJSONRPCServer((my_host, my_port)) server.register_function(process_request, 'request') server.register_function(do_dump, 'dump') server.register_function(getrequest, 'getrequest') server.register_function(do_stop, 'stop') server.socket.settimeout(1) while not stopping: try: server.handle_request() except socket.timeout: continue
class CTRexServer(object): """This class defines the server side of the RESTfull interaction with TRex""" TREX_START_CMD = './t-rex-64' DEFAULT_FILE_PATH = '/tmp/trex_files/' def __init__(self, trex_path, trex_files_path, trex_host='0.0.0.0', trex_daemon_port=8090, trex_zmq_port=4500, trex_nice=-19): """ Parameters ---------- trex_host : str a string of the TRex ip address or hostname. default value: machine hostname as fetched from socket.gethostname() trex_daemon_port : int the port number on which the trex-daemon server can be reached default value: 8090 trex_zmq_port : int the port number on which trex's zmq module will interact with daemon server default value: 4500 nice: int priority of the TRex process Instantiate a TRex client object, and connecting it to listening daemon-server """ self.TREX_PATH = os.path.abspath(os.path.dirname(trex_path + '/')) self.trex_files_path = os.path.abspath( os.path.dirname(trex_files_path + '/')) self.__check_trex_path_validity() self.__check_files_path_validity() self.trex = CTRex() self.trex_version = None self.trex_host = trex_host self.trex_daemon_port = trex_daemon_port self.trex_zmq_port = trex_zmq_port self.trex_server_path = "http://{hostname}:{port}".format( hostname=trex_host, port=trex_daemon_port) self.start_lock = threading.Lock() self.__reservation = None self.zmq_monitor = ZmqMonitorSession( self.trex, self.trex_zmq_port ) # intiate single ZMQ monitor thread for server usage self.trex_nice = int(trex_nice) if self.trex_nice < -20 or self.trex_nice > 19: err = "Parameter 'nice' should be integer in range [-20, 19]" print(err) logger.error(err) raise Exception(err) def add(self, x, y): logger.info( "Processing add function. Parameters are: {0}, {1} ".format(x, y)) return x + y # return Fault(-10, "") def push_file(self, filename, bin_data): logger.info("Processing push_file() command.") try: filepath = os.path.join(self.trex_files_path, os.path.basename(filename)) with open(filepath, 'wb') as f: f.write(binascii.a2b_base64(bin_data)) logger.info("push_file() command finished. File is saved as %s" % filepath) return True except IOError as inst: logger.error("push_file method failed. " + str(inst)) return False def connectivity_check(self): logger.info("Processing connectivity_check function.") return True def start(self): """This method fires up the daemon server based on initialized parameters of the class""" # initialize the server instance with given resources try: print("Firing up TRex REST daemon @ port {trex_port} ...\n".format( trex_port=self.trex_daemon_port)) logger.info( "Firing up TRex REST daemon @ port {trex_port} ...".format( trex_port=self.trex_daemon_port)) logger.info("current working dir is: {0}".format(self.TREX_PATH)) logger.info("current files dir is : {0}".format( self.trex_files_path)) logger.debug( "Starting TRex server. Registering methods to process.") logger.info(self.get_trex_version(base64=False)) self.server = SimpleJSONRPCServer( (self.trex_host, self.trex_daemon_port)) except socket.error as e: if e.errno == errno.EADDRINUSE: logger.error( "TRex server requested address already in use. Aborting server launching." ) print( "TRex server requested address already in use. Aborting server launching." ) raise socket.error( errno.EADDRINUSE, "TRex daemon requested address already in use. " "Server launch aborted. Please make sure no other process is " "using the desired server properties.") elif isinstance(e, socket.gaierror) and e.errno == -3: # handling Temporary failure in name resolution exception raise socket.gaierror( -3, "Temporary failure in name resolution.\n" "Make sure provided hostname has DNS resolving.") else: raise # set further functionality and peripherals to server instance self.server.register_function(self.add) self.server.register_function(self.cancel_reservation) self.server.register_function(self.connectivity_check) self.server.register_function(self.connectivity_check, 'check_connectivity') # alias self.server.register_function(self.force_trex_kill) self.server.register_function(self.get_file) self.server.register_function(self.get_files_list) self.server.register_function(self.get_files_path) self.server.register_function(self.get_latest_dump) self.server.register_function(self.get_running_info) self.server.register_function(self.get_running_status) self.server.register_function(self.get_trex_cmds) self.server.register_function(self.get_trex_config) self.server.register_function(self.get_trex_daemon_log) self.server.register_function(self.get_trex_log) self.server.register_function(self.get_trex_version) self.server.register_function(self.is_reserved) self.server.register_function(self.is_running) self.server.register_function(self.kill_all_trexes) self.server.register_function(self.push_file) self.server.register_function(self.reserve_trex) self.server.register_function(self.start_trex) self.server.register_function(self.stop_trex) self.server.register_function(self.wait_until_kickoff_finish) signal.signal(signal.SIGTSTP, self.stop_handler) signal.signal(signal.SIGTERM, self.stop_handler) try: self.zmq_monitor.start() self.server.serve_forever() except KeyboardInterrupt: logger.info("Daemon shutdown request detected.") finally: self.zmq_monitor.join() # close ZMQ monitor thread resources self.server.shutdown() #self.server.server_close() # get files from Trex server and return their content (mainly for logs) @staticmethod def _pull_file(filepath): try: with open(filepath, 'rb') as f: file_content = f.read() return binascii.b2a_base64(file_content).decode( errors='replace') except Exception as e: err_str = "Can't get requested file %s: %s" % (filepath, e) logger.error(err_str) return Fault(-33, err_str) # returns True if given path is under TRex package or under /tmp/trex_files def _check_path_under_TRex_or_temp(self, path): if not os.path.relpath(path, self.trex_files_path).startswith( os.pardir): return True if not os.path.relpath(path, self.TREX_PATH).startswith(os.pardir): return True return False # gets the file content encoded base64 either from /tmp/trex_files or TRex server dir def get_file(self, filepath): try: logger.info("Processing get_file() command.") if not self._check_path_under_TRex_or_temp(filepath): raise Exception( 'Given path should be under current TRex package or /tmp/trex_files' ) return self._pull_file(filepath) except Exception as e: err_str = "Can't get requested file %s: %s" % (filepath, e) logger.error(err_str) return Fault(-33, err_str) # get tuple (dirs, files) with directories and files lists from given path (limited under TRex package or /tmp/trex_files) def get_files_list(self, path): try: logger.info("Processing get_files_list() command, given path: %s" % path) if not self._check_path_under_TRex_or_temp(path): raise Exception( 'Given path should be under current TRex package or /tmp/trex_files' ) return os.walk(path).next()[1:3] except Exception as e: err_str = "Error processing get_files_list(): %s" % e logger.error(err_str) return Fault(-33, err_str) # get Trex log /tmp/trex.txt def get_trex_log(self): logger.info("Processing get_trex_log() command.") return self._pull_file('/tmp/trex.txt') # get /etc/trex_cfg.yaml def get_trex_config(self): logger.info("Processing get_trex_config() command.") return self._pull_file('/etc/trex_cfg.yaml') # get daemon log /var/log/trex/trex_daemon_server.log def get_trex_daemon_log(self): logger.info("Processing get_trex_daemon_log() command.") return self._pull_file('/var/log/trex/trex_daemon_server.log') # get Trex version from ./t-rex-64 --help (last lines starting with "Version : ...") def get_trex_version(self, base64=True): try: logger.info("Processing get_trex_version() command.") if not self.trex_version: ret_code, stdout, stderr = run_command('./t-rex-64 --help', cwd=self.TREX_PATH) search_result = re.search('\n\s*(Version\s*:.+)', stdout, re.DOTALL) if not search_result: raise Exception( 'Could not determine version from ./t-rex-64 --help') self.trex_version = binascii.b2a_base64( search_result.group(1).encode(errors='replace')) if base64: return self.trex_version.decode(errors='replace') else: return binascii.a2b_base64( self.trex_version).decode(errors='replace') except Exception as e: err_str = "Can't get trex version, error: %s" % e logger.error(err_str) return Fault(-33, err_str) def stop_handler(self, *args, **kwargs): logger.info("Daemon STOP request detected.") if self.is_running(): # in case TRex process is currently running, stop it before terminating server process self.stop_trex(self.trex.get_seq()) sys.exit(0) def assert_zmq_ok(self): if self.trex.zmq_error: self.trex.zmq_error, err = None, self.trex.zmq_error raise Exception('ZMQ thread got error: %s' % err) if not self.zmq_monitor.is_alive(): if self.trex.get_status() != TRexStatus.Idle: self.force_trex_kill() raise Exception('ZMQ thread is dead.') def is_running(self): run_status = self.trex.get_status() logger.info( "Processing is_running() command. Running status is: {stat}". format(stat=run_status)) if run_status == TRexStatus.Running: return True else: return False def is_reserved(self): logger.info("Processing is_reserved() command.") return bool(self.__reservation) def get_running_status(self): run_status = self.trex.get_status() logger.info( "Processing get_running_status() command. Running status is: {stat}" .format(stat=run_status)) return { 'state': run_status.value, 'verbose': self.trex.get_verbose_status() } def get_files_path(self): logger.info("Processing get_files_path() command.") return self.trex_files_path def reserve_trex(self, user): if user == "": logger.info( "TRex reservation cannot apply to empty string user. Request denied." ) return Fault( -33, "TRex reservation cannot apply to empty string user. Request denied." ) with self.start_lock: logger.info("Processing reserve_trex() command.") if self.is_reserved(): if user == self.__reservation['user']: # return True is the same user is asking and already has the resrvation logger.info( "the same user is asking and already has the resrvation. Re-reserving TRex." ) return True logger.info( "TRex is already reserved to another user ({res_user}), cannot reserve to another user." .format(res_user=self.__reservation['user'])) return Fault( -33, "TRex is already reserved to another user ({res_user}). Please make sure TRex is free before reserving it." .format(res_user=self.__reservation['user'] )) # raise at client TRexInUseError elif self.trex.get_status() != TRexStatus.Idle: logger.info( "TRex is currently running, cannot reserve TRex unless in Idle state." ) return Fault( -13, 'TRex is currently running, cannot reserve TRex unless in Idle state. Please try again when TRex run finished.' ) # raise at client TRexInUseError else: logger.info( "TRex is now reserved for user ({res_user}).".format( res_user=user)) self.__reservation = {'user': user, 'since': time.ctime()} logger.debug("Reservation details: " + str(self.__reservation)) return True def cancel_reservation(self, user): with self.start_lock: logger.info("Processing cancel_reservation() command.") if self.is_reserved(): if self.__reservation['user'] == user: logger.info( "TRex reservation to {res_user} has been canceled successfully." .format(res_user=self.__reservation['user'])) self.__reservation = None return True else: logger.warning( "TRex is reserved to different user than the provided one. Reservation wasn't canceled." ) return Fault( -33, "Cancel reservation request is available to the user that holds the reservation. Request denied" ) # raise at client TRexRequestDenied else: logger.info( "TRex is not reserved to anyone. No need to cancel anything" ) assert (self.__reservation is None) return False def start_trex(self, trex_cmd_options, user, block_to_success=True, timeout=40, stateless=False, debug_image=False, trex_args=''): self.trex.zmq_error = None self.assert_zmq_ok() with self.start_lock: logger.info("Processing start_trex() command.") if self.is_reserved(): # check if this is not the user to which TRex is reserved if self.__reservation['user'] != user: logger.info( "TRex is reserved to another user ({res_user}). Only that user is allowed to initiate new runs." .format(res_user=self.__reservation['user'])) return Fault( -33, "TRex is reserved to another user ({res_user}). Only that user is allowed to initiate new runs." .format(res_user=self.__reservation['user'] )) # raise at client TRexRequestDenied elif self.trex.get_status() != TRexStatus.Idle: err = 'TRex is already taken, cannot create another run until done.' logger.info(err) return Fault(-13, err) # raise at client TRexInUseError try: server_cmd_data = self.generate_run_cmd( stateless=stateless, debug_image=debug_image, trex_args=trex_args, **trex_cmd_options) self.zmq_monitor.first_dump = True self.trex.start_trex(self.TREX_PATH, server_cmd_data) logger.info("TRex session has been successfully initiated.") if block_to_success: # delay server response until TRex is at 'Running' state. start_time = time.time() trex_state = None while (time.time() - start_time) < timeout: trex_state = self.trex.get_status() if trex_state != TRexStatus.Starting: break else: time.sleep(0.5) self.assert_zmq_ok() # check for TRex run started normally if trex_state == TRexStatus.Starting: # reached timeout logger.warning( "TimeoutError: TRex initiation outcome could not be obtained, since TRex stays at Starting state beyond defined timeout." ) return Fault( -12, 'TimeoutError: TRex initiation outcome could not be obtained, since TRex stays at Starting state beyond defined timeout.' ) # raise at client TRexWarning elif trex_state == TRexStatus.Idle: return Fault(-11, self.trex.get_verbose_status() ) # raise at client TRexError # reach here only if TRex is at 'Running' state self.trex.gen_seq() return self.trex.get_seq( ) # return unique seq number to client except TypeError as e: logger.error( "TRex command generation failed, probably because either -f (traffic generation .yaml file) and -c (num of cores) was not specified correctly.\nReceived params: {params}" .format(params=trex_cmd_options)) raise TypeError( 'TRex -f (traffic generation .yaml file) and -c (num of cores) must be specified. %s' % e) def stop_trex(self, seq): logger.info("Processing stop_trex() command.") if self.trex.get_seq() == seq: logger.debug("Abort request legit since seq# match") return self.trex.stop_trex() else: if self.trex.get_status() != TRexStatus.Idle: logger.warning( "Abort request is only allowed to process initiated the run. Request denied." ) return Fault( -33, 'Abort request is only allowed to process initiated the run. Request denied.' ) # raise at client TRexRequestDenied else: return False def force_trex_kill(self): logger.info( "Processing force_trex_kill() command. --> Killing TRex session indiscriminately." ) return self.trex.stop_trex() # returns list of tuples (pid, command line) of running TRex(es) def get_trex_cmds(self): logger.info('Processing get_trex_cmds() command.') ret_code, stdout, stderr = run_command( 'ps -u root --format pid,comm,cmd') if ret_code: raise Exception( 'Failed to determine running processes, stderr: %s' % stderr) trex_cmds_list = [] for line in stdout.splitlines(): pid, proc_name, full_cmd = line.strip().split(' ', 2) pid = pid.strip() full_cmd = full_cmd.strip() if proc_name.find('_t-rex-64') >= 0: trex_cmds_list.append((pid, full_cmd)) return trex_cmds_list # Silently tries to kill TRexes with given signal. # Responsibility of client to verify with get_trex_cmds. def kill_all_trexes(self, signal_name): logger.info('Processing kill_all_trexes() command.') trex_cmds_list = self.get_trex_cmds() for pid, cmd in trex_cmds_list: logger.info('Killing with signal %s process %s %s' % (signal_name, pid, cmd)) try: os.kill(int(pid), signal_name) except OSError as e: if e.errno == errno.ESRCH: logger.info('No such process, ignoring.') raise def wait_until_kickoff_finish(self, timeout=40): # block until TRex exits Starting state logger.info("Processing wait_until_kickoff_finish() command.") start_time = time.time() while (time.time() - start_time) < timeout: self.assert_zmq_ok() trex_state = self.trex.get_status() if trex_state != TRexStatus.Starting: return time.sleep(0.1) return Fault( -12, 'TimeoutError: TRex initiation outcome could not be obtained, since TRex stays at Starting state beyond defined timeout.' ) # raise at client TRexWarning def get_running_info(self): self.assert_zmq_ok() logger.info("Processing get_running_info() command.") return self.trex.get_running_info() def get_latest_dump(self): logger.info("Processing get_latest_dump() command.") return self.trex.get_latest_dump() def generate_run_cmd(self, iom=0, export_path="/tmp/trex.txt", stateless=False, debug_image=False, trex_args='', **kwargs): """ generate_run_cmd(self, iom, export_path, kwargs) -> str Generates a custom running command for the kick-off of the TRex traffic generator. Returns a tuple of command (string) and export path (string) to be issued on the trex server Parameters ---------- iom: int 0 = don't print stats screen to log, 1 = print stats (can generate huge logs) stateless: boolean True = run as stateless, False = require -f and -d arguments kwargs: dictionary Dictionary of parameters for trex. For example: (c=1, nc=True, l_pkt_mode=3). Notice that when sending command line parameters that has -, you need to replace it with _. for example, to have on command line "--l-pkt-mode 3", you need to send l_pkt_mode=3 export_path : str Full system path to which the results of the trex-run will be logged. """ if 'results_file_path' in kwargs: export_path = kwargs['results_file_path'] del kwargs['results_file_path'] if stateless: kwargs['i'] = True # adding additional options to the command trex_cmd_options = '' for key, value in kwargs.items(): tmp_key = key.replace('_', '-').lstrip('-') dash = ' -' if (len(key) == 1) else ' --' if value is True: trex_cmd_options += (dash + tmp_key) elif value is False: continue else: trex_cmd_options += (dash + '{k} {val}'.format(k=tmp_key, val=value)) if trex_args: trex_cmd_options += ' %s' % trex_args self._check_zmq_port(trex_cmd_options) if not stateless: if 'f' not in kwargs: raise Exception( 'Argument -f should be specified in stateful command') if 'd' not in kwargs: raise Exception( 'Argument -d should be specified in stateful command') cmd = "{nice}{run_command}{debug_image} --iom {io} {cmd_options} --no-key".format( # -- iom 0 disables the periodic log to the screen (not needed) nice='' if self.trex_nice == 0 else 'nice -n %s ' % self.trex_nice, run_command=self.TREX_START_CMD, debug_image='-debug' if debug_image else '', cmd_options=trex_cmd_options, io=iom) logger.info("TREX FULL COMMAND: {command}".format(command=cmd)) return (cmd, export_path, kwargs.get('d', 0)) def _check_zmq_port(self, trex_cmd_options): zmq_cfg_port = 4500 # default parser = ArgumentParser() parser.add_argument('--cfg', default='/etc/trex_cfg.yaml') args, _ = parser.parse_known_args(shlex.split(trex_cmd_options)) if not os.path.exists(args.cfg): raise Exception('Platform config file "%s" does not exist!' % args.cfg) with open(args.cfg) as f: trex_cfg = yaml.safe_load(f.read()) if type(trex_cfg) is not list: raise Exception( 'Platform config file "%s" content should be array.' % args.cfg) if not len(trex_cfg): raise Exception( 'Platform config file "%s" content should be array with one element.' % args.cfg) trex_cfg = trex_cfg[0] if 'enable_zmq_pub' in trex_cfg and trex_cfg['enable_zmq_pub'] == False: raise Exception( 'TRex daemon expects ZMQ publisher to be enabled. Please change "enable_zmq_pub" to true.' ) if 'zmq_pub_port' in trex_cfg: zmq_cfg_port = trex_cfg['zmq_pub_port'] if zmq_cfg_port != self.trex_zmq_port: raise Exception( 'ZMQ port does not match: platform config file is configured to: %s, daemon server to: %s' % (zmq_cfg_port, self.trex_zmq_port)) def __check_trex_path_validity(self): # check for executable existance if not os.path.exists(self.TREX_PATH + '/t-rex-64'): print( "The provided TRex path do not contain an executable TRex file.\nPlease check the path and retry." ) logger.error( "The provided TRex path do not contain an executable TRex file" ) exit(-1) # check for executable permissions st = os.stat(self.TREX_PATH + '/t-rex-64') if not bool(st.st_mode & (stat.S_IXUSR)): print( "The provided TRex path do not contain an TRex file with execution privileges.\nPlease check the files permissions and retry." ) logger.error( "The provided TRex path do not contain an TRex file with execution privileges" ) exit(-1) else: return def __check_files_path_validity(self): # first, check for path existance. otherwise, try creating it with appropriate credentials if not os.path.exists(self.trex_files_path): try: os.makedirs(self.trex_files_path, 0o660) return except os.error as inst: print( "The provided files path does not exist and cannot be created with needed access credentials using root user.\nPlease check the path's permissions and retry." ) logger.error( "The provided files path does not exist and cannot be created with needed access credentials using root user." ) exit(-1) elif os.access(self.trex_files_path, os.W_OK): return else: print( "The provided files path has insufficient access credentials for root user.\nPlease check the path's permissions and retry." ) logger.error( "The provided files path has insufficient access credentials for root user" ) exit(-1)
def start(self): """This method fires up the daemon server based on initialized parameters of the class""" # initialize the server instance with given resources try: print("Firing up TRex REST daemon @ port {trex_port} ...\n".format( trex_port=self.trex_daemon_port)) logger.info( "Firing up TRex REST daemon @ port {trex_port} ...".format( trex_port=self.trex_daemon_port)) logger.info("current working dir is: {0}".format(self.TREX_PATH)) logger.info("current files dir is : {0}".format( self.trex_files_path)) logger.debug( "Starting TRex server. Registering methods to process.") logger.info(self.get_trex_version(base64=False)) self.server = SimpleJSONRPCServer( (self.trex_host, self.trex_daemon_port)) except socket.error as e: if e.errno == errno.EADDRINUSE: logger.error( "TRex server requested address already in use. Aborting server launching." ) print( "TRex server requested address already in use. Aborting server launching." ) raise socket.error( errno.EADDRINUSE, "TRex daemon requested address already in use. " "Server launch aborted. Please make sure no other process is " "using the desired server properties.") elif isinstance(e, socket.gaierror) and e.errno == -3: # handling Temporary failure in name resolution exception raise socket.gaierror( -3, "Temporary failure in name resolution.\n" "Make sure provided hostname has DNS resolving.") else: raise # set further functionality and peripherals to server instance self.server.register_function(self.add) self.server.register_function(self.cancel_reservation) self.server.register_function(self.connectivity_check) self.server.register_function(self.connectivity_check, 'check_connectivity') # alias self.server.register_function(self.force_trex_kill) self.server.register_function(self.get_file) self.server.register_function(self.get_files_list) self.server.register_function(self.get_files_path) self.server.register_function(self.get_latest_dump) self.server.register_function(self.get_running_info) self.server.register_function(self.get_running_status) self.server.register_function(self.get_trex_cmds) self.server.register_function(self.get_trex_config) self.server.register_function(self.get_trex_daemon_log) self.server.register_function(self.get_trex_log) self.server.register_function(self.get_trex_version) self.server.register_function(self.is_reserved) self.server.register_function(self.is_running) self.server.register_function(self.kill_all_trexes) self.server.register_function(self.push_file) self.server.register_function(self.reserve_trex) self.server.register_function(self.start_trex) self.server.register_function(self.stop_trex) self.server.register_function(self.wait_until_kickoff_finish) signal.signal(signal.SIGTSTP, self.stop_handler) signal.signal(signal.SIGTERM, self.stop_handler) try: self.zmq_monitor.start() self.server.serve_forever() except KeyboardInterrupt: logger.info("Daemon shutdown request detected.") finally: self.zmq_monitor.join() # close ZMQ monitor thread resources self.server.shutdown()
def __init__(self, config, fd): DaemonThread.__init__(self) self.config = config if config.get('offline'): self.network = None else: self.network = Network(config) self.network.start() self.gui = None self.wallets = {} # Setup JSONRPC server path = config.get_wallet_path() default_wallet = self.load_wallet(path) cmd_runner = Commands(self.config, default_wallet, self.network) host = config.get('rpchost', 'localhost') port = config.get('rpcport', 0) server = SimpleJSONRPCServer((host, port), logRequests=False, requestHandler=RequestHandler) os.write(fd, repr((server.socket.getsockname(), time.time()))) os.close(fd) server.timeout = 0.1 for cmdname in known_commands: server.register_function(getattr(cmd_runner, cmdname), cmdname) server.register_function(self.run_cmdline, 'run_cmdline') server.register_function(self.ping, 'ping') server.register_function(self.run_daemon, 'daemon') server.register_function(self.run_gui, 'gui') self.server = server
def main(): server = SimpleJSONRPCServer(('localhost', 12000)) server.register_function(execute) print("Start server") server.serve_forever()
def run_server(port = 8095): native_methods = [ 'acquire', 'connect', 'disconnect', 'get_stats', 'get_warnings', 'push_remote', 'reset', 'wait_on_traffic', ] hltapi_methods = [ 'connect', 'cleanup_session', 'interface_config', 'traffic_config', 'traffic_control', 'traffic_stats', ] try: server = SimpleJSONRPCServer(('0.0.0.0', port)) server.register_function(add) server.register_function(check_connectivity) server.register_function(native_proxy_init) server.register_function(native_proxy_del) server.register_function(hltapi_proxy_init) server.register_function(hltapi_proxy_del) server.register_function(native_method) server.register_function(hltapi_method) for method in native_methods: server.register_function(partial(native_method, method), method) for method in hltapi_methods: if method in native_methods: # collision in names method_hlt_name = 'hlt_%s' % method else: method_hlt_name = method server.register_function(partial(hltapi_method, method), method_hlt_name) server.register_function(server.funcs.keys, 'get_methods') # should be last print('Started Stateless RPC proxy at port %s' % port) server.serve_forever() except KeyboardInterrupt: print('Done')
def ping(): return True setSpeed(40) for x in xrange(_Servo0UL, _Servo0LL, -1): setCam0Position(x) sleep(.01) for x in xrange(_Servo0LL, 50): setCam0Position(x) sleep(.01) if __name__ == "__main__": server = SimpleJSONRPCServer((([ (s.connect(('8.8.8.8', 80)), s.getsockname()[0], s.close()) for s in [socket.socket(socket.AF_INET, socket.SOCK_DGRAM)] ][0][1]), 10101)) server.register_function(setSpeed) server.register_function(stop) server.register_function(goForward) server.register_function(goBackward) server.register_function(pivotLeft) server.register_function(pivotRight) server.register_function(ping) server.register_function(setCam0Position) try: print "Server listening..." server.serve_forever() except KeyboardInterrupt:
default=DIRECTORY, help='Stanford CoreNLP tool directory (default %s)' % DIRECTORY) parser.add_option( '-P', '--properties', default='default.properties', help='Stanford CoreNLP properties fieles (default: default.properties)' ) options, args = parser.parse_args() VERBOSE = options.verbose # server = jsonrpc.Server(jsonrpc.JsonRpc20(), # jsonrpc.TransportTcpIp(addr=(options.host, int(options.port)))) try: if not options.ports: server = SimpleJSONRPCServer((options.host, int(options.port))) nlp = StanfordCoreNLP(options.corenlp, properties=options.properties) server.register_function(nlp.parse) print 'Serving on http://%s:%s' % (options.host, options.port) server.serve_forever() else: server = SimpleJSONRPCServer((options.host, int(options.port))) lb = CoreNLPLoadBalancer(options) server.register_function(lb.send) server.register_function(lb.getAll) server.register_function(lb.getCompleted) server.register_function(lb.getForKey)
parser.add_option('-S', '--corenlp', default=DIRECTORY, help='Stanford CoreNLP tool directory (default %s)' % DIRECTORY) parser.add_option( '-P', '--properties', default='default.properties', help='Stanford CoreNLP properties fieles (default: default.properties)' ) options, args = parser.parse_args() VERBOSE = options.verbose # server = jsonrpc.Server(jsonrpc.JsonRpc20(), # jsonrpc.TransportTcpIp(addr=(options.host, int(options.port)))) try: server = SimpleJSONRPCServer((options.host, int(options.port))) nlp = StanfordCoreNLP(options.corenlp, properties=options.properties, serving=True) server.register_function(nlp.parse) server.register_function(nlp.raw_parse) print 'Serving on http://%s:%s' % (options.host, options.port) # server.serve() server.serve_forever() except KeyboardInterrupt: print >> sys.stderr, "Bye." exit()
logging.basicConfig(format=LOGGER_FORMAT) LOGGER = logging.getLogger('backend_server') LOGGER.setLevel(logging.DEBUG) def get_preference_for_user(user_id): """ Get user's preference in an ordered class list. """ LOGGER.debug("get_preference_for_user is called with %s", str(user_id)) db = mongodb_client.get_db() model = db[PREFERENCE_MODEL_TABLE_NAME].find_one({'userId': user_id}) if model is None: return [] sorted_tuples = sorted(list(model['preference'].items()), key=operator.itemgetter(1), reverse=True) sorted_list = [x[0] for x in sorted_tuples] sorted_value_list = [x[1] for x in sorted_tuples] if math.isclose(float(sorted_value_list[0]), float(sorted_value_list[-1])): return [] return sorted_list # Threading RPC server. RPC_SERVER = SimpleJSONRPCServer((SERVER_HOST, SERVER_PORT)) RPC_SERVER.register_function(get_preference_for_user, 'getPreferenceForUser') LOGGER.info("Starting RPC server on %s:%d", SERVER_HOST, SERVER_PORT) RPC_SERVER.serve_forever()
def get_one_news(): print("getOneNews is called...") news = operations.getOneNews() return json.loads(dumps(news)) def get_news_summaries_for_user(user_id, page_num): """ get news summaries for a user """ print("get_news_summaries_for_user is called with %s and %s" % (user_id, page_num)) return operations.getNewsSummariesForUser(user_id, page_num) def log_news_click_for_user(user_id, news_id): """ send click news log from a user """ print("log_news_click_for_user is called with %s and %s" % (user_id, news_id)) operations.logNewsClickForUser(user_id, news_id) server = SimpleJSONRPCServer((SERVER_HOST, SERVER_PORT)) server.register_function(add, 'add') server.register_function(get_one_news, 'getOneNews') server.register_function(get_news_summaries_for_user, 'getNewsSummariesForUser') server.register_function(log_news_click_for_user, 'logNewsClickForUser') print("Starting RPC server on %s:%d" % (SERVER_HOST, SERVER_PORT)) server.serve_forever()
def main(): server = SimpleJSONRPCServer(('localhost', 7002)) server.register_function(lengths) print("Starting server") server.serve_forever()
def init_server(self, config, fd, is_gui): host = config.get('rpchost', '127.0.0.1') port = config.get('rpcport', 0) try: server = SimpleJSONRPCServer((host, port), logRequests=False) except Exception as e: self.print_error('Warning: cannot initialize RPC server on host', host, e) self.server = None os.close(fd) return os.write( fd, bytes(repr((server.socket.getsockname(), time.time())), 'utf8')) os.close(fd) self.server = server server.timeout = 0.1 server.register_function(self.ping, 'ping') if is_gui: server.register_function(self.run_gui, 'gui') else: server.register_function(self.run_daemon, 'daemon') self.cmd_runner = Commands(self.config, None, self.network) for cmdname in known_commands: server.register_function(getattr(self.cmd_runner, cmdname), cmdname) server.register_function(self.run_cmdline, 'run_cmdline') self.server = server