def __init__(self, init_mode, host, port=MPM_RPC_PORT, token=None): assert isinstance(init_mode, InitMode) print("[MPMRPC] Attempting to connect to {host}:{port}...".format( host=host, port=port)) self._remote_methods = [] if init_mode == InitMode.Hijack: assert token self._token = token if init_mode == InitMode.Claim: self._claimer = MPMClaimer(host, port) self._claimer.claim() self._token = self._claimer.token try: self._client = RPCClient(host, port, pack_params={'use_bin_type': True}) print("[MPMRPC] Connection successful.") except Exception as ex: print("[MPMRPC] Connection refused: {}".format(ex)) raise RuntimeError("RPC connection refused.") print("[MPMRPC] Getting methods...") methods = self._client.call('list_methods') for method in methods: self._add_command(*method) print("[MPMRPC] Added {} methods.".format(len(methods)))
def complete(self, context, prefix): from mprpc import RPCClient client = RPCClient('localhost', self.__port) response = client.call('complete', context, prefix) return [decode_completion(r) for r in response]
def segment(self, text): from mprpc import RPCClient client = RPCClient('localhost', self.__port) response = client.call('segment', text) return [decode_segment(r) for r in response]
def connect(self, host, port): """ Launch a connection. """ print("Attempting to connect to {host}:{port}...".format(host=host, port=port)) try: self.client = RPCClient(host, port, pack_params={'use_bin_type': True}) print("Connection successful.") except Exception as ex: print("Connection refused") print("Error: {}".format(ex)) return False self._host = host self._port = port print("Getting methods...") methods = self.client.call('list_methods') for method in methods: self._add_command(*method) print("Added {} methods.".format(len(methods))) print("Quering device info...") self._device_info = self.client.call('get_device_info') return True
def call_register(uuid): print("Registering peer with uuid: {}".format(uuid)) client = RPCClient(b'localhost', 18080) res = client.call(b'register', uuid) client.close() return res
def __init__(self, host="127.0.0.1", port=9033, model="en", embeddings_path=None, verbose=False, attributes=None): super(Client, self).__init__(model, embeddings_path) self.host = host self.port = port self.rpc = RPCClient(host, port) self.verbose = verbose self.attributes = attributes self.cache = LRUCache(maxsize=3000000)
class MPMClient: """ MPM RPC Client: Will make all MPM commands accessible as Python methods. """ def __init__(self, init_mode, host, port, token=None): assert isinstance(init_mode, InitMode) print("[MPMRPC] Attempting to connect to {host}:{port}...".format( host=host, port=port )) try: self._client = RPCClient(host, port, pack_params={'use_bin_type': True}) print("[MPMRPC] Connection successful.") except Exception as ex: print("[MPMRPC] Connection refused: {}".format(ex)) raise RuntimeError("RPC connection refused.") self._remote_methods = [] if init_mode == InitMode.Hijack: assert token self._token = token if init_mode == InitMode.Claim: self._claimer = MPMClaimer(host, port) self._claimer.claim() self._token = self._claimer.token print("[MPMRPC] Getting methods...") methods = self._client.call('list_methods') for method in methods: self._add_command(*method) print("[MPMRPC] Added {} methods.".format(len(methods))) def _add_command(self, command, docs, requires_token): """ Add a command to the current session """ if not hasattr(self, command): new_command = lambda *args, **kwargs: self._rpc_template( str(command), requires_token, *args, **kwargs ) new_command.__doc__ = docs setattr(self, command, new_command) self._remote_methods.append(command) def _rpc_template(self, command, requires_token, *args, **kwargs): """ Template function to create new RPC shell commands """ if requires_token and not self._token: raise RuntimeError( "[MPMRPC] Cannot execute `{}' -- no claim available!" .format(command)) # Put token as the first argument if required: if requires_token: args = (self._token,) + args if kwargs: return self._client.call(command, *args, **kwargs) if args: return self._client.call(command, *args) return self._client.call(command)
def call(): from mprpc import RPCClient client = RPCClient('ipc://x.sock') start = time.time() [client.call('sum', 1, 2) for _ in xrange(NUM_CALLS)] print 'call: %d qps' % (NUM_CALLS / (time.time() - start))
def call(): from mprpc import RPCClient client = RPCClient('127.0.0.1', 6000) start = time.time() [client.call('sum', 1, 2) for _ in xrange(NUM_CALLS)] print 'call: %d qps' % (NUM_CALLS / (time.time() - start))
def rpc(address, port, command, *args): if not port: port = mpm.types.MPM_RPC_PORT client = RPCClient(address, port) if args: args = [eval(arg.lstrip("=")) if arg.startswith("=") else arg for arg in args] result = client.call(command, *args) else: result = client.call(command) return result
def call_get_schedule(uuid, num, method="smart"): print("Getting schedule for uuid: {}".format(uuid)) data = {'uuid': uuid, 'num': num, 'method': method} client = RPCClient(b'localhost', 18080) result = client.call(b'get_schedule', data) client.close() return result
def __init__(self, host="127.0.0.1", port=9033, model="en", embeddings_path=None, verbose=False): super(Client, self).__init__(model, embeddings_path) self.host = host self.port = port self.rpc = RPCClient(host, port) self.verbose = verbose
def rpc(address, port, command, *args): if not port: port = mpm.types.MPM_RPC_PORT client = RPCClient(address, port) if args: args = [ eval(arg.lstrip("=")) if arg.startswith("=") else arg for arg in args ] result = client.call(command, *args) else: result = client.call(command) return result
def connect(self): try: if not self.OK: self._link_ = RPCClient(self.host, int(self.port), timeout=20) if self._link_.is_connected(): self.OK = self.__create_hooks() else: self.OK = False except: self.OK = False if self.showerr: P_Log("[FR]ERROR [FW] No Connect to {}".format(uri)) finally: return self.OK
def call_get_schedule(uuid, num, method="smart"): print("Getting schedule for uuid: {}".format(uuid)) data = { 'uuid':uuid, 'num':num, 'method':method } client = RPCClient(b'localhost', 18080) result = client.call(b'get_schedule', data) client.close() return result
def call(): from mprpc import RPCClient client = RPCClient('127.0.0.1', 7777) t = np.zeros(NUM_CALLS) for _ in range(NUM_CALLS): t1 = time.time() client.call('sum', 1, 2) t2 = time.time() t[_] = t2 - t1 print('stdev: {}'.format(t.std())) print('mean: {}'.format(t.mean())) print('call: {} qps'.format(1 / t.mean()))
def __init__(self, uri, show=False): self.showerr = show try: self.host, self.port = uri.split(":") except: self.host = "0.0.0.0" self.port = 0 self.OK = False self.__linked = False try: self._link_ = RPCClient(self.host, int(self.port), timeout=10) self.__linked = True except: self.__linked = False if show: P_Log("[FR]ERROR [FW] No Link to {}".format(uri))
def test_server(self, server): class Sum: def sum(self, x, y): return x + y server.load(Sum) self.g = gevent.spawn(server.run) sleep(0.1) registry = server.registry res = registry.discovery(server.name) key = registry._node_key(server.name, url.get_param("node")) host, port = res[key].split(":") client = RPCClient(str(host), int(port)) assert client.call("sum", {}, 1, 2) == 3
class CVPChatClient: Client = RPCClient('127.0.0.1', 20181) while (True): Client.call('SendMessage', "Hello") Client.call('SendMessage', "JAM") Client.call('SendMessage', "x3") print(Client.call('GetRecentMessage')) time.sleep(1)
class BitFlyerMarket(BitFlyerMarketBase): def __init__(self, symbol=SYMBOL, resolution=60, port=PROXY_PORT): super().__init__(symbol, resolution) self.client = RPCClient('127.0.0.1', port) self.data = self.client.call('ohlcv', TICKERID, self.resolution, 256) def step_ohlcv(self, next_clock): d1, d0 = self.client.call('step_ohlcv', TICKERID, self.resolution, next_clock) if d1 is None: return None if d0['t'] <= self.data['t'][-1]: return None for k, v in d0.items(): self.data[k].pop(0) self.data[k][-1] = d1[k] self.data[k].append(v) return d0['t']
def __init__(self, host, port): self.token = None self._cmd_q = multiprocessing.Queue() self._token_q = multiprocessing.Queue() client = RPCClient(host, port, pack_params={'use_bin_type': True}) self._claim_loop = multiprocessing.Process(target=_claim_loop, name="Claimer Loop", args=(client, self._cmd_q, self._token_q)) self._claim_loop.daemon = True self._claim_loop.start()
def _claim_loop(host, port, cmd_q, token_q): """ Process that runs a claim loop. This function should be run in its own thread. Communication to the outside thread happens with two queues: A command queue, and a token queue. The command queue is used to pass in one of these commands: claim, unclaim, or exit. The token queue is used to read back the current token. """ command = None token = None exit_loop = False client = RPCClient(host, port, pack_params={'use_bin_type': True}) try: while not exit_loop: if token and not command: client.call('reclaim', token) elif command == 'claim': if not token: token = client.call('claim', 'UHD') else: print("Already have claim") token_q.put(token) elif command == 'unclaim': if token: client.call('unclaim', token) token = None token_q.put(None) elif command == 'exit': if token: client.call('unclaim', token) token = None token_q.put(None) exit_loop = True time.sleep(1) command = None if not cmd_q.empty(): command = cmd_q.get(False) except RPCError as ex: print("Unexpected RPC error in claimer loop!") print(str(ex))
def claim_loop(self, host, port, cmd_q, token_q): """ Run a claim loop """ from mprpc import RPCClient from mprpc.exceptions import RPCError command = None token = None exit_loop = False client = RPCClient(host, port, pack_params={'use_bin_type': True}) try: while not exit_loop: if token and not command: client.call('reclaim', token) elif command == 'claim': if not token: token = client.call('claim', 'MPM Shell') else: print("Already have claim") token_q.put(token) elif command == 'unclaim': if token: client.call('unclaim', token) token = None token_q.put(None) elif command == 'exit': if token: client.call('unclaim', token) token = None token_q.put(None) exit_loop = True time.sleep(1) command = None if not cmd_q.empty(): command = cmd_q.get(False) except RPCError as ex: print("Unexpected RPC error in claimer loop!") print(str(ex))
def claim_loop(self, host, port, disc_callback): """ Run a claim loop """ client = RPCClient(host, port, pack_params={'use_bin_type': True}) self.token = client.call('claim', 'MPM Shell') try: while not self._exit_loop: client.call('reclaim', self.token) time.sleep(1) client.call('unclaim', self.token) except RPCError as ex: print("Unexpected RPC error in claimer loop!") print(str(ex)) disc_callback() self.token = None
def __exec_mprpc__(self, args): from mprpc import RPCClient, RPCPoolClient method = args[0] args = args[1] assert method in ["get", "set"] try: client = RPCClient(self.server, self.mprpc_port) result = client.call(method, args) client.close() return result except Exception as e: print(e) try: client.close() except: pass
class Client(BaseClient): def __init__(self, host="127.0.0.1", port=9033, model="en", embeddings_path=None, verbose=False): super(Client, self).__init__(model, embeddings_path) self.host = host self.port = port self.rpc = RPCClient(host, port) self.verbose = verbose def _call(self, path, *args): return self.rpc.call(path, *args) @cachetools.func.lru_cache(maxsize=3000000) def single(self, document, attributes=None): sentences = self._call("single", document, self.model, self.embeddings_path, attributes) return SpacyClientDocument(sentences) def _bulk(self, documents, attributes): return self._call("bulk", documents, self.model, self.embeddings_path, attributes) def bulk(self, documents, batch_size=1000, attributes=None): parsed_documents = [] if len(documents) > batch_size: batches = int(math.ceil(len(documents) / batch_size)) print("Batching {} requests with batch_size {}".format( batches, batch_size)) if self.verbose: batch_iterator = tqdm.tqdm(range(batches)) else: batch_iterator = range(batches) for b in batch_iterator: docs = documents[b * batch_size:(b + 1) * batch_size] res = self._bulk(docs, attributes) parsed_documents.extend(res) else: parsed_documents = self._bulk(documents, attributes) return [SpacyClientDocument(x) for x in parsed_documents]
def connect(self, host, port): """ Launch a connection. """ print("Attempting to connect to {host}:{port}...".format( host=host, port=port )) try: self.client = RPCClient(host, port, pack_params={'use_bin_type': True}) print("Connection successful.") except Exception as ex: print("Connection refused") print("Error: {}".format(ex)) return False self._host = host self._port = port print("Getting methods...") methods = self.client.call('list_methods') for method in methods: self._add_command(*method) print("Added {} methods.".format(len(methods))) return True
class Proxy(object): def __init__(self, uri, show=False): self.showerr = show try: self.host, self.port = uri.split(":") except: self.host = "0.0.0.0" self.port = 0 self.OK = False self.__linked = False try: self._link_ = RPCClient(self.host, int(self.port), timeout=10) self.__linked = True except: self.__linked = False if show: P_Log("[FR]ERROR [FW] No Link to {}".format(uri)) def _close(self): if self._link_.is_connected(): self._link_.close() def connect(self): try: if not self.OK: self._link_ = RPCClient(self.host, int(self.port), timeout=20) if self._link_.is_connected(): self.OK = self.__create_hooks() else: self.OK = False except: self.OK = False if self.showerr: P_Log("[FR]ERROR [FW] No Connect to {}".format(uri)) finally: return self.OK def __call__(self): return self.connect() def __str__(self): if self.OK: return "{}:{}::Connected".format(*self._link_.getpeername()) elif self.__linked: return "{}:{}::Linked".format(*self._link_.getpeername()) else: return "Disconected 1.1.1.1:1" def __create_hooks(self): try: _config = self._link_.call('G_E_T_Configuration') except: _config = {} #print("error creating hooks {}:{}".format(self.host,self.port)) hooks = [] for defs, params in _config.items(): params_def = params[0] params_call = "" for x in params[1]: params_call = params_call + "," + str(x) d = def_skel.format(defs, params_def, params_call, self.host + ":" + str(self.port)) #print(d) hooks.append((defs, d)) for defs, fun in hooks: exec(fun) self.__dict__[defs] = types.MethodType(eval(defs), self) self.functions = hooks return len(self.functions) > 0 def get_uri(self): return "{}:{}".format(self.host, self.port) def get_functions(self): return [a for a, b in self.functions] def call(self, fn, *args): try: return self._link_.call(fn, *args) except Exception as ex: #raise P_Log("[[FR]ERROR[FW]] Running call on Proxy {}".format( self.get_uri())) P_Log(str(ex)) return None
from mprpc import RPCClient client = RPCClient('localhost', 6666 ) #o = client.call('sum', 1, 2) s = client.call('sleep', 10)
class URLClientRemote(object): """ The URLClient takes URLs and sends them to the URLServer to get their IDs or fields We should migrate to something more efficient (but more complex!) like gRPC in the future. """ urlserver = None def connect(self): self.urlserver = RPCClient( config["URLSERVER"].split(":")[0], int(config["URLSERVER"].split(":")[1]) ) def empty(self): pass def get_id(self, url): """ Simple API to get the ID of an URL """ return self.get_ids([url])[0] def get_domain_id(self, url): """ Simple API to get the ID of a domain """ return self.get_domain_ids([url])[0] def _rpc(self, method, *args): return self.urlserver.call(method, *args) def get_ids(self, urls): """ Returns an array of 64-bit integers for each URL """ if len(urls) == 0: return [] if isinstance(urls[0], URL): urls = [u.url for u in urls] return self._rpc("get_ids", urls) def get_domain_ids(self, urls): """ Returns an array of 64-bit integers for each domain """ if len(urls) == 0: return [] if isinstance(urls[0], URL): urls = [u.url for u in urls] return self._rpc("get_domain_ids", urls) def get_metadata(self, urls): """ Returns an array of complete fields for each URL """ if len(urls) == 0: return [] if isinstance(urls[0], URL): urls = [u.url for u in urls] res = self._rpc("get_metadata", urls) ret = [] for row in res: d = {} for i, f in enumerate(URLSERVER_FIELDNAMES["get_metadata"]): d[f] = row[i] ret.append(d) return ret
class MPMShell(cmd.Cmd): """ RPC Shell class. See cmd module. """ def __init__(self): cmd.Cmd.__init__(self) self.prompt = "> " self.client = None self.remote_methods = [] self._claimer = None self._host = None self._port = None self._device_info = None args = parse_args() if args.host is not None: self.connect(args.host, args.port) if args.claim: self.claim() elif args.hijack: self.hijack(args.hijack) self.update_prompt() def _add_command(self, command, docs, requires_token=False): """ Add a command to the current session """ cmd_name = 'do_' + command if not hasattr(self, cmd_name): new_command = lambda args: self.rpc_template( str(command), requires_token, args ) new_command.__doc__ = docs setattr(self, cmd_name, new_command) self.remote_methods.append(command) def rpc_template(self, command, requires_token, args=None): """ Template function to create new RPC shell commands """ if requires_token and \ (self._claimer is None or self._claimer.token is None): print("Cannot execute `{}' -- no claim available!") return try: if args or requires_token: expanded_args = self.expand_args(args) if requires_token: expanded_args.insert(0, self._claimer.token) response = self.client.call(command, *expanded_args) else: response = self.client.call(command) except RPCError as ex: print("RPC Command failed!") print("Error: {}".format(ex)) return except Exception as ex: print("Unexpected exception!") print("Error: {}".format(ex)) return if isinstance(response, bool): if response: print("Command executed successfully!") return print("Command failed!") return print("==> " + str(response)) def get_names(self): " We need this for tab completion. " return dir(self) ########################################################################### # Cmd module specific ########################################################################### def run(self): " Go, go, go! " try: self.cmdloop() except KeyboardInterrupt: self.do_disconnect(None) exit(0) def postcmd(self, stop, line): """ Is run after every command executes. Does: - Update prompt """ self.update_prompt() ########################################################################### # Internal methods ########################################################################### def connect(self, host, port): """ Launch a connection. """ print("Attempting to connect to {host}:{port}...".format( host=host, port=port )) try: self.client = RPCClient(host, port, pack_params={'use_bin_type': True}) print("Connection successful.") except Exception as ex: print("Connection refused") print("Error: {}".format(ex)) return False self._host = host self._port = port print("Getting methods...") methods = self.client.call('list_methods') for method in methods: self._add_command(*method) print("Added {} methods.".format(len(methods))) print("Quering device info...") self._device_info = self.client.call('get_device_info') return True def disconnect(self): """ Clean up after a connection was closed. """ self._device_info = None if self._claimer is not None: self._claimer.unclaim() if self.client: try: self.client.close() except RPCError as ex: print("Error while closing the connection") print("Error: {}".format(ex)) for method in self.remote_methods: delattr(self, "do_" + method) self.remote_methods = [] self.client = None self._host = None self._port = None def claim(self): " Initialize claim " assert self.client is not None if self._claimer is not None: print("Claimer already active.") return True print("Claiming device...") self._claimer = MPMClaimer(self._host, self._port, self.unclaim_hook) return True def hijack(self, token): " Hijack running session " assert self.client is not None if self._claimer is not None: print("Claimer already active. Can't hijack.") return False print("Hijacking device...") self._claimer = MPMHijacker(token) return True def unclaim(self): """ unclaim """ if self._claimer is not None: self._claimer.unclaim() self._claimer = None def unclaim_hook(self): """ Hook """ pass def update_prompt(self): """ Update prompt """ if self._device_info is None: self.prompt = '> ' else: if self._claimer is None: claim_status = '' elif isinstance(self._claimer, MPMClaimer): claim_status = ' [C]' elif isinstance(self._claimer, MPMHijacker): claim_status = ' [H]' self.prompt = '{dev_id}{claim_status}> '.format( dev_id=self._device_info.get( 'name', self._device_info.get('serial', '?') ), claim_status=claim_status, ) def expand_args(self, args): """ Takes a string and returns a list """ if self._claimer is not None and self._claimer.token is not None: args = args.replace('$T', str(self._claimer.token)) eval_preamble = '=' args = args.strip() if args.startswith(eval_preamble): parsed_args = eval(args.lstrip(eval_preamble)) if not isinstance(parsed_args, list): parsed_args = [parsed_args] else: parsed_args = [] for arg in args.split(): try: parsed_args.append(int(arg, 0)) continue except ValueError: pass try: parsed_args.append(float(arg)) continue except ValueError: pass parsed_args.append(arg) return parsed_args ########################################################################### # Predefined commands ########################################################################### def do_connect(self, args): """ Connect to a remote MPM server. See connect() """ host, port = split_args(args, 'localhost', MPM_RPC_PORT) port = int(port) self.connect(host, port) def do_claim(self, _): """ Spawn a claim loop """ self.claim() def do_hijack(self, token): """ Hijack a running session """ self.hijack(token) def do_unclaim(self, _): """ unclaim """ self.unclaim() def do_disconnect(self, _): """ disconnect from the RPC server """ self.disconnect() def do_import(self, args): """import a python module into the global namespace""" globals()[args] = import_module(args) def do_EOF(self, _): " When catching EOF, exit the program. " print("Exiting...") self.disconnect() exit(0)
def callraspi(): client = RPCClient("133.27.171.245", 6000) print(client.call('unlock_key'))
# -*- coding: utf-8 -*- """如果想用gevent, 可以用gevent patch all """ from mprpc import RPCClient client = RPCClient('tcp://127.0.0.1:6000', reconnect_delay=5) print client.call('sum', 1, 2)
class URLClientRemote(object): """ The URLClient takes URLs and sends them to the URLServer to get their IDs or fields We should migrate to something more efficient (but more complex!) like gRPC in the future. """ urlserver = None def connect(self): self.urlserver = RPCClient( config["URLSERVER"].split(":")[0], int(config["URLSERVER"].split(":")[1]) ) def empty(self): pass def get_id(self, url): """ Simple API to get the ID of an URL """ return self.get_ids([url])[0] def get_domain_id(self, url): """ Simple API to get the ID of a domain """ return self.get_domain_ids([url])[0] def _rpc(self, method, *args): return self.urlserver.call(method, *args) def get_ids(self, urls): """ Returns an array of 64-bit integers for each URL """ if len(urls) == 0: return [] if isinstance(urls[0], URL): urls = [u.url for u in urls] return self._rpc("get_ids", urls) def get_domain_ids(self, urls): """ Returns an array of 64-bit integers for each domain """ if len(urls) == 0: return [] if isinstance(urls[0], URL): urls = [u.url for u in urls] return self._rpc("get_domain_ids", urls) def get_metadata(self, urls): """ Returns an array of complete fields for each URL, for each main variation of those URLs """ if len(urls) == 0: return [] if not isinstance(urls[0], URL): urls = [URL(u) for u in urls] # Multiple variations of the URLs to be tested. It is slightly inefficient, but we # send the 4 variations in the same request and split them just below. urls_variations = [] for url in urls: urls_variations.extend([ url.normalized, url.normalized_without_query, url.normalized_domain, url.pld ]) res = self._rpc("get_metadata", urls_variations) ret = [] for url_i in py2_xrange(len(res) // 4): url_metadata = {} for i, key in ((0, "url"), (1, "url_without_query"), (2, "domain"), (3, "pld")): url_metadata[key] = urlserver_pb2.UrlMetadata() url_metadata[key].ParseFromString(res[(url_i * 4) + i]) ret.append(url_metadata) return ret
def call(): client = RPCClient('127.0.0.1', 6000) client.open() print client.call('sum', 1, 2)
#import pyjsonrpc from mprpc import RPCClient ''' from paramiko import SSHClient from scp import SCPClient ssh = SSHClient() ssh.load_system_host_keys() ssh.connect('192.168.1.16', username='******', password='******', look_for_keys=False) with SCPClient(ssh.get_transport()) as scp: scp.put("/home/pi/Desktop/pyUI/profiles/aaa/top/aaa.jpg", "/home/pi/Desktop/aaa.jpg") ''' client = RPCClient('127.0.0.1', 8080) print(client.call('add', 1, 2)) print(client.call('updateProfile', "")) print(client.call('profilepath', '/home/pi/Desktop/pyUI/profiles', 'aaa')) print(client.call('ResultTest', 5)) #client.call('Init') client.call('TakePicture', 0, False) client.call('TakePicture', 1) client.call('TakePicture', 2) client.call('CreateSamplePoint', 0, 150, 200) #client.call('Uninit') client.call('CloseServer') ''' http_client = pyjsonrpc.HttpClient( url = "http://localhost:8080/jsonrpc" #username = "******",
class MPMShell(cmd.Cmd): """ RPC Shell class. See cmd module. """ def __init__(self, host, port, claim, hijack): cmd.Cmd.__init__(self) self.prompt = "> " self.client = None self.remote_methods = [] self._claimer = None self._host = host self._port = port self._device_info = None if host is not None: self.connect(host, port) if claim: self.claim() elif hijack: self.hijack(hijack) self.update_prompt() def _add_command(self, command, docs, requires_token=False): """ Add a command to the current session """ cmd_name = 'do_' + command if not hasattr(self, cmd_name): new_command = lambda args: self.rpc_template( str(command), requires_token, args ) new_command.__doc__ = docs setattr(self, cmd_name, new_command) self.remote_methods.append(command) def rpc_template(self, command, requires_token, args=None): """ Template function to create new RPC shell commands """ if requires_token and \ (self._claimer is None or self._claimer.token is None): print("Cannot execute `{}' -- no claim available!") return try: if args or requires_token: expanded_args = self.expand_args(args) if requires_token: expanded_args.insert(0, self._claimer.token) response = self.client.call(command, *expanded_args) else: response = self.client.call(command) except RPCError as ex: print("RPC Command failed!") print("Error: {}".format(ex)) return except Exception as ex: print("Unexpected exception!") print("Error: {}".format(ex)) return if isinstance(response, bool): if response: print("Command executed successfully!") else: print("Command failed!") else: print("==> " + str(response)) return response def get_names(self): " We need this for tab completion. " return dir(self) ########################################################################### # Cmd module specific ########################################################################### def run(self): " Go, go, go! " try: self.cmdloop() except KeyboardInterrupt: self.do_disconnect(None) exit(0) def postcmd(self, stop, line): """ Is run after every command executes. Does: - Update prompt """ self.update_prompt() ########################################################################### # Internal methods ########################################################################### def connect(self, host, port): """ Launch a connection. """ print("Attempting to connect to {host}:{port}...".format( host=host, port=port )) try: self.client = RPCClient(host, port, pack_params={'use_bin_type': True}) print("Connection successful.") except Exception as ex: print("Connection refused") print("Error: {}".format(ex)) return False self._host = host self._port = port print("Getting methods...") methods = self.client.call('list_methods') for method in methods: self._add_command(*method) print("Added {} methods.".format(len(methods))) print("Quering device info...") self._device_info = self.client.call('get_device_info') return True def disconnect(self): """ Clean up after a connection was closed. """ self._device_info = None if self._claimer is not None: self._claimer.unclaim() if self.client: try: self.client.close() except RPCError as ex: print("Error while closing the connection") print("Error: {}".format(ex)) for method in self.remote_methods: delattr(self, "do_" + method) self.remote_methods = [] self.client = None self._host = None self._port = None def claim(self): " Initialize claim " assert self.client is not None if self._claimer is not None: print("Claimer already active.") return True print("Claiming device...") self._claimer = MPMClaimer(self._host, self._port, self.unclaim_hook) return True def hijack(self, token): " Hijack running session " assert self.client is not None if self._claimer is not None: print("Claimer already active. Can't hijack.") return False print("Hijacking device...") self._claimer = MPMHijacker(token) return True def unclaim(self): """ unclaim """ if self._claimer is not None: self._claimer.unclaim() self._claimer = None def unclaim_hook(self): """ Hook """ pass def update_prompt(self): """ Update prompt """ if self._device_info is None: self.prompt = '> ' else: if self._claimer is None: claim_status = '' elif isinstance(self._claimer, MPMClaimer): claim_status = ' [C]' elif isinstance(self._claimer, MPMHijacker): claim_status = ' [H]' self.prompt = '{dev_id}{claim_status}> '.format( dev_id=self._device_info.get( 'name', self._device_info.get('serial', '?') ), claim_status=claim_status, ) def expand_args(self, args): """ Takes a string and returns a list """ if self._claimer is not None and self._claimer.token is not None: args = args.replace('$T', str(self._claimer.token)) eval_preamble = '=' args = args.strip() if args.startswith(eval_preamble): parsed_args = eval(args.lstrip(eval_preamble)) if not isinstance(parsed_args, list): parsed_args = [parsed_args] else: parsed_args = [] for arg in args.split(): try: parsed_args.append(int(arg, 0)) continue except ValueError: pass try: parsed_args.append(float(arg)) continue except ValueError: pass parsed_args.append(arg) return parsed_args ########################################################################### # Predefined commands ########################################################################### def do_connect(self, args): """ Connect to a remote MPM server. See connect() """ host, port = split_args(args, 'localhost', MPM_RPC_PORT) port = int(port) self.connect(host, port) def do_claim(self, _): """ Spawn a claim loop """ self.claim() def do_hijack(self, token): """ Hijack a running session """ self.hijack(token) def do_unclaim(self, _): """ unclaim """ self.unclaim() def do_disconnect(self, _): """ disconnect from the RPC server """ self.disconnect() def do_import(self, args): """import a python module into the global namespace""" globals()[args] = import_module(args) def do_EOF(self, _): " When catching EOF, exit the program. " print("Exiting...") self.disconnect() exit(0)
def call(): client = RPCClient('ipc://x.sock', reconnect_delay=5) print client.call('sum', 1, 2)
def connect(self): self.urlserver = RPCClient( config["URLSERVER"].split(":")[0], int(config["URLSERVER"].split(":")[1]) )
def call(): client = RPCClient('127.0.0.1', 6000) print client.call('sum', 1, 2)
from mprpc import RPCClient client = RPCClient('127.0.0.1', 6000) print client.call('add', 1000, 1000)