def __registration(self): port = self.__listening_port if ((self.__bc_ipaddr == None) or \ (self.__bc_listening_port == None)): print "no BC ip or port specified, skipping registration" self.__registration_loop.stop() return bc_register = Proxy('http://%s:%d/' % (self.__bc_ipaddr, self.__bc_listening_port)) if self.__bc_connected: d = bc_register.callRemote('keepalive', self.__local_ip, port) else: host_specs = HostSpecsManager().get_host_specs() specs = jsonpickle.encode(host_specs) from core.blockinfo import block_infos blocks = block_infos.keys() blocks_descr = [block_infos[b] for b in blocks] descr = jsonpickle.encode(blocks_descr) #blocks_descr = [str(block_infos[b]) for b in blocks] #d = bc_register.callRemote('register', self.__local_ip, port, specs, blocks) #d = bc_register.callRemote('register', self.__local_ip, port, specs, blocks, blocks_descr) d = bc_register.callRemote('register', self.__local_ip, port, specs, blocks, descr) d.addCallback(self.__registration_results).addErrback( self.__registration_error)
class CoinSwapJSONRPCClient(object): """A class encapsulating Alice's json rpc client. """ #Keys map to states as per description of CoinswapAlice method_names = { 0: "handshake", 1: "negotiate", 3: "tx0id_hx_tx2sig", 5: "sigtx3", 9: "secret", 12: "sigtx4" } def __init__(self, host, port, json_callback=None, backout_callback=None, usessl=False): self.host = host self.port = int(port) #Callback fired on receiving response to send() self.json_callback = json_callback #Callback fired on receiving any response failure self.backout_callback = backout_callback if usessl: self.proxy = Proxy('https://' + host + ":" + str(port) + "/", ssl_ctx_factory=AltCtxFactory) else: self.proxy = Proxy('http://' + host + ":" + str(port) + "/") def error(self, errmsg): """error callback implies we must back out at this point. Note that this includes stateless queries, as any malformed or non-response must be interpreted as malicious. """ self.backout_callback(str(errmsg)) def send_poll(self, method, callback, noncesig, sessionid, *args): """Stateless queries during the run use this call, and provide their own callback for the response. """ d = self.proxy.callRemote("coinswap", sessionid, noncesig, method, *args) d.addCallback(callback).addErrback(self.error) def send_poll_unsigned(self, method, callback, *args): """Stateless queries outside of a coinswap run use this query method; no nonce, sessionid or signature needed. """ d = self.proxy.callRemote(method, *args) d.addCallback(callback).addErrback(self.error) def send(self, method, *args): """Stateful queries share the same callback: the state machine update function. """ d = self.proxy.callRemote(method, *args) d.addCallback(self.json_callback).addErrback(self.error)
class ShutterJSONRPCTestCase(unittest.TestCase): def setUp(self): self.port = reactor.listenTCP(8001, RemoteShutterFactory(ShutterJSONRPCProtocol())) self.client = Proxy('http://127.0.0.1:8001', version=jsonrpclib.VERSION_2) self.conn = psycopg2.connect(config.DB_CONN) self.cur = self.conn.cursor() if not os.path.exists("static"): os.makedirs("static") def tearDown(self): self.cur.execute("TRUNCATE TABLE shutter.urls CASCADE") self.conn.commit() self.port.stopListening() self.conn.close() for target in os.listdir("static"): os.unlink("static/%s" % target) os.rmdir("static") def test_snapshots_empty(self): def _result(value): self.assertTrue(value['error'] is None) self.assertFalse(value['result'] is None) self.assertEqual(value['result'], []) return self.client.callRemote("snapshots", "http://programistamag.pl").addCallback(_result) def test_snapshots_fake_entries(self): def _result(value): # Check the return values self.assertEqual(value['result'], ["fake3.png", "fake2.png", "fake1.png"]) # Add fake entries into the database url = "http://fake.test.com" self.cur.execute("""INSERT INTO shutter.urls(url) VALUES (%s) RETURNING id """, [url]) url_id = self.cur.fetchone()[0] def _make_file(fakeFile): self.cur.execute("""INSERT INTO shutter.snapshots(url_id, file_path) VALUES (%s, %s)""", [url_id, fakeFile]) self.conn.commit() _make_file("fake1.png") _make_file("fake2.png") _make_file("fake3.png") return self.client.callRemote("snapshots", "http://fake.test.com").addCallback(_result) def test_snapshot(self): def _result(value): self.assertTrue(value['error'] is None) self.assertFalse(value['result'] is None) # The file name is 36 characters long, 32 for the checksum # and 4 characters for the extension and dot. self.assertEqual(len(value['result']), 36) return self.client.callRemote("snapshot", "http://programistamag.pl").addCallback(_result)
def testjsonrpc(com1,address,port,arglist): jsonaddress="http://"+str(address)+":"+str(port)+"/" proxy = Proxy(jsonaddress) if (len(arglist)==0): d = proxy.callRemote(com1) elif (len(arglist)==1): d = proxy.callRemote(com1,arglist[0]) elif (len(arglist)==2): d = proxy.callRemote(com1,arglist[0],arglist[1]) elif (len(arglist)==3): d = proxy.callRemote(com1,arglist[0],arglist[1],arglist[2]) else: print "[Error] - with the arguments for the jsonrpc command" d.addCallback(printValue).addErrback(printError).addBoth(shutDown)
class RegisterTester(unittest.TestCase): #note: setUp is called right before each test def setUp(self): self.NODE_PORT = 7080 self.REG_PORT = 7090 self.daemon = Proxy('http://127.0.0.1:%d/' % self.REG_PORT) self.PORTS = range(5050,5058) # REGISTER def test_register(self): import random if random.randint(0,10) <=5: ports = self.PORTS else: ports = range(5070,5075) cpus,cores,memory = 2, 1,400 specs = [cpus, cores, memory] d = self.daemon.callRemote('register','127.0.1.1',self.NODE_PORT, ports, specs) d.addCallback(print_results) return d def test_register2(self): cpus,cores,memory = 2, 1,400 specs = [cpus, cores, memory] d = self.daemon.callRemote('register','127.0.0.1',self.NODE_PORT+1, self.PORTS, specs) d.addCallback(print_results) return d def test_unregister(self): d = self.daemon.callRemote('unregister','10.1.2.137',self.NODE_PORT) d.addCallback(print_results) return d def test_keepalive(self): d = self.daemon.callRemote('keepalive','127.0.0.1',self.NODE_PORT, self.PORTS) d.addCallback(print_results) return d def test_keepalive_rand(self): import random port = random.randint(100,200) d = self.daemon.callRemote('keepalive','127.0.0.1',port, self.PORTS) d.addCallback(print_results) return d #test_unregister.skip = "disabled locally" test_register.skip = "disabled locally" test_register2.skip = "disabled locally" test_keepalive.skip = "disabled locally" test_keepalive_rand.skip = "disabled locally"
def proxy_upsert(lst): splitted = defaultdict(list) defers = list() g = GlobalConfig() for item in lst: range_ = find_searcher_by_path(item['path']) splitted[range_].append(item) for range_, searcher in g.searcher.iteritems(): url = searcher['url'] p = RPCProxy(path_join(url, 'op')) if splitted[range_]: defers.append(p.callRemote('upsert', splitted[range_])) try: writer_result = yield defer.DeferredList(defers) affected = filter(lambda x: x[0], writer_result) result = reduce(lambda x, y: x + y, map( lambda x: x[1]['affected'], affected), 0) except Exception as e: raise e defer.returnValue(result)
def async_DELETE(self, request): path_ = path_reverse(self.domain) q = '__path__ <@ %s.__data__ AND zone == %s' % (path_, self.domain) #q = '__path__ ~ %s.__data__.* AND zone == %s' % (path_, self.domain) result = yield search_all(q) if len(result) > 1: defer.returnValue(RESTResult(code=400, content=dict( status='fail', reason='zone contains RR cannot be deleted.'))) zpath_ = path_ + '.__data__.SOA.0' defers = list() g = GlobalConfig() #INFO: send DELETE to all searchers for range_, searcher in g.searcher.iteritems(): url = searcher['url'] p = RPCProxy(path_join(url, 'op')) defers.append(p.callRemote('delete', [zpath_])) try: rlist = yield defer.DeferredList(defers) rlist = filter(lambda x: x[0], rlist) result = reduce(lambda x, y: x+y, map(lambda x: x[1]['affected'], rlist), 0) except Exception as e: raise(e) #NOTE: code204'll cause returned json disappeared. Find why. #defer.returnValue(RESTResult(code=204, content=dict(affected=result))) defer.returnValue(RESTResult(code=200, content=dict(affected=result)))
def jsonrpc_get(self, lst, method='self'): g = GlobalConfig() splitted = defaultdict(list) defers = list() for item in lst: range_ = self.find_searcher_by_path(item) splitted[range_].append(item) for range_, searcher in g.searcher.iteritems(): url = g.searcher[range_]['url'] p = RPCProxy(path_join(url, 'op')) if splitted[range_]: defers.append(p.callRemote('get', splitted[range_], method)) try: reader_result = yield defer.DeferredList(defers) returns = map(lambda x: x[1], filter(lambda x: x[0], reader_result)) # http://stackoverflow.com/questions/952914/making-a-flat-list-out-of-list-of-lists-in-python result = [item for sublist in returns for item in sublist] except Exception as e: raise e defer.returnValue(result)
class HypheConnection: def __init__(self, address): self.proxy = Proxy(address) def run_command(self, command, args): d = self.proxy.callRemote(command, *args) d.addCallback(print_value).addErrback(print_error) d.addCallback(shutdown) reactor.run()
def __registration(self): port = self.__listening_port if ((self.__bc_ipaddr == None) or \ (self.__bc_listening_port == None)): print "no BC ip or port specified, skipping registration" self.__registration_loop.stop() return bc_register = Proxy('http://%s:%d/' % (self.__bc_ipaddr,self.__bc_listening_port)) if self.__bc_connected: d = bc_register.callRemote('keepalive', self.__local_ip, port) else: host_specs = HostSpecsManager().get_host_specs() specs = jsonpickle.encode(host_specs) from core.blockinfo import block_infos blocks = block_infos.keys() blocks_descr = [block_infos[b] for b in blocks] descr = jsonpickle.encode(blocks_descr) #blocks_descr = [str(block_infos[b]) for b in blocks] #d = bc_register.callRemote('register', self.__local_ip, port, specs, blocks) #d = bc_register.callRemote('register', self.__local_ip, port, specs, blocks, blocks_descr) d = bc_register.callRemote('register', self.__local_ip, port, specs, blocks, descr) d.addCallback(self.__registration_results).addErrback(self.__registration_error)
def search_all(q): result, defers = list(), list() for range_, searcher in GlobalConfig().searcher.iteritems(): url = searcher['url'] p = RPCProxy(path_join(url, 'op')) defers.append(p.callRemote('search', q)) try: reader_result = yield defer.DeferredList(defers) succeed = filter(lambda x: x[0], reader_result) map(lambda x: result.extend(x[1]), succeed) except Exception as e: raise e defer.returnValue(result)
class NodeTester(unittest.TestCase): #note: setUp is called right before each test def setUp(self): self.NODE_PORT = 7080 self.daemon = Proxy('http://127.0.0.1:%d/' % self.NODE_PORT) self.comp = 'step1' # NODE def test_read_vars(self): v1 = ["counter", "pktcnt", "", "read"] v2 = ["counter", "bytecnt", "", "read"] variables = [v1, v2] d = self.daemon.callRemote('read_variables', self.comp, variables) d.addCallback(print_results) return d
def jsonrpc_search(self, query): g = GlobalConfig() result = list() defers = list() for range_, searcher in g.searcher.iteritems(): url = searcher['url'] out('searcher: ' + url) p = RPCProxy(path_join(url, 'op')) defers.append(p.callRemote('search', query)) try: reader_result = yield defer.DeferredList(defers) succeed = filter(lambda x: x[0], reader_result) map(lambda x: result.extend(x[1]), succeed) except Exception as e: raise e defer.returnValue(result)
def main(): parser = argparse.ArgumentParser(description="Send an rpc command to a dht node") parser.add_argument("rpc_command", help="The rpc command to send to the dht node") parser.add_argument("--node_host", help="The host of the node to connect to", default="127.0.0.1") parser.add_argument("--node_port", help="The port of the node to connect to", default="8888") args = parser.parse_args() connect_string = 'http://%s:%s' % (args.node_host, args.node_port) proxy = Proxy(connect_string) d = proxy.callRemote(args.rpc_command) d.addCallbacks(print_value, print_error) d.addBoth(lambda _: shut_down()) reactor.run()
class BitcoinRpc(object): def __init__(self, config_file, timeout=None): self.timeout = timeout with open(config_file) as f: content = f.read().splitlines() config = { split[0]: split[1] for split in [row.split('=', 1) for row in content if len(row)] } if 'testnet' in config: port = 18332 else: port = 8332 rpcuser = config['rpcuser'] rpcpassword = config['rpcpassword'] self.proxy = Proxy('http://%s:%[email protected]:%d' % (rpcuser, rpcpassword, port)) def __getattr__(self, key): if key.startswith("__"): raise AttributeError def proxy_method(*args, **kwargs): d = self.proxy.callRemote(key, *args, **kwargs) if self.timeout is not None: timeout = reactor.callLater( self.timeout, d.errback, BitcoinRpcTimeout("Bitcoin call timed out: %s" % key)) def cancelTimeout(result): if timeout.active(): timeout.cancel() return result d.addBoth(cancelTimeout) return d return proxy_method
class BitcoinRpc(object): def __init__(self, config_file, timeout=None): self.timeout = timeout with open(config_file) as f: content = f.read().splitlines() config = {split[0]: split[1] for split in [row.split('=', 1) for row in content if len(row)]} if 'testnet' in config: port = 18332 else: port = 8332 rpcuser = config['rpcuser'] rpcpassword = config['rpcpassword'] self.proxy = Proxy('http://%s:%[email protected]:%d' % (rpcuser, rpcpassword, port)) def __getattr__(self, key): if key.startswith("__"): raise AttributeError def proxy_method(*args, **kwargs): d = self.proxy.callRemote(key, *args, **kwargs) if self.timeout is not None: timeout = reactor.callLater(self.timeout, d.errback, BitcoinRpcTimeout("Bitcoin call timed out: %s" % key)) def cancelTimeout(result): if timeout.active(): timeout.cancel() return result d.addBoth(cancelTimeout) return d return proxy_method
class StoreMessage(object): message = None proxy = None value = None def __init__(self, message, proxy=None): if type(message) == str: self.message = message else: raise Exception("Only strings are excepted") if not proxy: self.proxy = Proxy("%s:%d/" % (settings.TWISTED_HOST, settings.TWISTED_PORT)) def setValueAndStop(self, value): self.value = value reactor.stop() def printError(self, error): print error def run(self): defer = self.proxy.callRemote("logMessage", self.message) defer.addCallback(self.setValueAndStop)
dl.append(d) d = proxy.callRemote('math.add', 3, 5) d.addCallbacks(printValue, printError) dl.append(d) d = proxy.callRemote('science.compare', 5, 7) d.addCallbacks(printValue, printError) dl.append(d) d = proxy.callRemote('science.compare', 7, 5) d.addCallbacks(printValue, printError) dl.append(d) """ d = proxy.callRemote('stikies.get', "will") d.addCallbacks(printValue, printError) dl.append(d) d = proxy.callRemote('stikies.add', "will1", "message1") d.addCallbacks(printValue, printError) dl.append(d) d = proxy.callRemote('stikies.add', "will", "message") d.addCallbacks(printValue, printError) dl.append(d) d = proxy.callRemote('stikies.add', "will2", "message2") d.addCallbacks(printValue, printError) dl.append(d)
from txjsonrpc.web.jsonrpc import Proxy def printValue(value): print "Result: %s" % str(value) def printError(error): print 'error', error def shutDown(data): print "Shutting down reactor..." reactor.stop() proxy = Proxy('http://127.0.0.1:6969/') dl = [] d = proxy.callRemote('echo', 'bite me') d.addCallbacks(printValue, printError) dl.append(d) d = proxy.callRemote('math.add', 3, 5) d.addCallbacks(printValue, printError) dl.append(d) dl = defer.DeferredList(dl) dl.addCallback(shutDown) reactor.run()
status, message = error.value.args if status == "401": print message return print 'error', error def shutDown(data): print "Shutting down reactor..." reactor.stop() proxyUnauth = Proxy('http://127.0.0.1:6969/') dl = [] d = proxyUnauth.callRemote('echo', 'bite me') d.addCallbacks(printValue, printError) dl.append(d) d = proxyUnauth.callRemote('math.add', 3, 5) d.addCallbacks(printValue, printError) dl.append(d) proxyAuth = Proxy('http://*****:*****@127.0.0.1:6969/') d = proxyAuth.callRemote('echo', 'bite me') d.addCallbacks(printValue, printError) dl.append(d) d = proxyAuth.callRemote('math.add', 3, 5) d.addCallbacks(printValue, printError)
def shut_down(data): print "Shutting down reactor..." reactor.stop() def shut_down_harsh(data): print "Shutting down reactor..." os._exit(1) bm_daemon = Proxy('http://127.0.0.1:' + str(DAEMON_PORT) + '/') value = int(sys.argv[1]) if value == 1: d = bm_daemon.callRemote('get_blocks_list') d.addCallback(print_results).addErrback(print_error).addBoth(shut_down) elif value == 2: d = bm_daemon.callRemote('get_hw_specs') d.addCallback(print_results).addErrback(print_error).addBoth(shut_down) elif value == 3: b = ['SketchMerger', 'L4Demux', 'PcapSource', 'ComboSZE2Source', 'PerFlowStats', 'PktPairifier', 'Null', 'SynthSource', 'CDFGenerator', 'SynFloodDetector', 'SketchFlowCounter', 'PacketPrinter', 'TCPFlagCounter', 'PktCounter', 'IPFIXExporter', 'RRDemux', 'TopNFlowSelector', 'PFQSource', 'IpDumbAnonymizer', 'IPFIXSource', 'PFRingSource'] d = bm_daemon.callRemote('get_blocks_info', b) d.addCallback(print_block_infos).addErrback(print_error).addBoth(shut_down) elif value == 4: f = open("/home/fhuici/research/eu/demons/svn/Sources/blockmon/main/node/daemon/tests/compositions/snifferctr.xml", "r") c = f.read() f.close()
def verifyCallback(connection, x509, errnum, errdepth, ok): log.msg(connection.__str__()) if not ok: log.msg('invalid server cert: %s' % x509.get_subject(), logLevel=logging.ERROR) return False else: log.msg('good server cert: %s' % x509.get_subject(), logLevel=logging.INFO) return True class AltCtxFactory(ssl.ClientContextFactory): def getContext(self): #self.method = SSL.SSLv23_METHOD ctx = ssl.ClientContextFactory.getContext(self) ctx.set_verify(SSL.VERIFY_PEER, verifyCallback) ctx.load_verify_locations("cacert.pem") #ctx.use_certificate_file('keys/client.crt') #ctx.use_privatekey_file('keys/client.key') return ctx import sys log.startLogging(sys.stdout) #proxy = Proxy('https://127.0.0.1:7443/', ssl_ctx_factory=AltCtxFactory) proxy = Proxy('https://127.0.0.2:7443/', ssl_ctx_factory=AltCtxFactory) d = proxy.callRemote('add', 3, 5) d.addCallback(printValue).addErrback(printError).addBoth(shutDown) reactor.run()
class TestTagpoolApiServer(VumiTestCase): @inlineCallbacks def setUp(self): self.persistence_helper = self.add_helper(PersistenceHelper()) self.redis = yield self.persistence_helper.get_redis_manager() self.tagpool = TagpoolManager(self.redis) site = Site(TagpoolApiServer(self.tagpool)) self.server = yield reactor.listenTCP(0, site, interface='127.0.0.1') self.add_cleanup(self.server.loseConnection) addr = self.server.getHost() self.proxy = Proxy("http://%s:%d/" % (addr.host, addr.port)) yield self.setup_tags() @inlineCallbacks def setup_tags(self): # pool1 has two tags which are free yield self.tagpool.declare_tags([("pool1", "tag1"), ("pool1", "tag2")]) # pool2 has two tags which are used yield self.tagpool.declare_tags([("pool2", "tag1"), ("pool2", "tag2")]) yield self.tagpool.acquire_specific_tag(["pool2", "tag1"]) yield self.tagpool.acquire_specific_tag(["pool2", "tag2"]) # pool3 is empty but has metadata yield self.tagpool.set_metadata("pool3", {"meta": "data"}) def _check_reason(self, result, expected_owner, expected_reason): owner, reason = result self.assertEqual(owner, expected_owner) self.assertEqual(reason.pop('owner'), expected_owner) self.assertTrue(isinstance(reason.pop('timestamp'), float)) self.assertEqual(reason, expected_reason) @inlineCallbacks def test_acquire_tag(self): result = yield self.proxy.callRemote("acquire_tag", "pool1") self.assertEqual(result, ["pool1", "tag1"]) self.assertEqual((yield self.tagpool.inuse_tags("pool1")), [("pool1", "tag1")]) result = yield self.proxy.callRemote("acquire_tag", "pool2") self.assertEqual(result, None) @inlineCallbacks def test_acquire_tag_with_owner_and_reason(self): result = yield self.proxy.callRemote("acquire_tag", "pool1", "me", {"foo": "bar"}) self.assertEqual(result, ["pool1", "tag1"]) result = yield self.tagpool.acquired_by(["pool1", "tag1"]) self._check_reason(result, "me", {"foo": "bar"}) @inlineCallbacks def test_acquire_specific_tag(self): result = yield self.proxy.callRemote("acquire_specific_tag", ["pool1", "tag1"]) self.assertEqual(result, ["pool1", "tag1"]) self.assertEqual((yield self.tagpool.inuse_tags("pool1")), [("pool1", "tag1")]) result = yield self.proxy.callRemote("acquire_specific_tag", ["pool2", "tag1"]) self.assertEqual(result, None) @inlineCallbacks def test_acquire_specific_tag_with_owner_and_reason(self): result = yield self.proxy.callRemote("acquire_specific_tag", ["pool1", "tag1"], "me", {"foo": "bar"}) self.assertEqual(result, ["pool1", "tag1"]) result = yield self.tagpool.acquired_by(["pool1", "tag1"]) self._check_reason(result, "me", {"foo": "bar"}) @inlineCallbacks def test_release_tag(self): result = yield self.proxy.callRemote("release_tag", ["pool1", "tag1"]) self.assertEqual(result, None) result = yield self.proxy.callRemote("release_tag", ["pool2", "tag1"]) self.assertEqual(result, None) self.assertEqual((yield self.tagpool.inuse_tags("pool2")), [("pool2", "tag2")]) @inlineCallbacks def test_declare_tags(self): tags = [("newpool", "tag1"), ("newpool", "tag2")] result = yield self.proxy.callRemote("declare_tags", tags) self.assertEqual(result, None) free_tags = yield self.tagpool.free_tags("newpool") self.assertEqual(sorted(free_tags), sorted(tags)) @inlineCallbacks def test_get_metadata(self): result = yield self.proxy.callRemote("get_metadata", "pool3") self.assertEqual(result, {"meta": "data"}) result = yield self.proxy.callRemote("get_metadata", "pool1") self.assertEqual(result, {}) @inlineCallbacks def test_set_metadata(self): result = yield self.proxy.callRemote("set_metadata", "newpool", {"my": "data"}) self.assertEqual(result, None) self.assertEqual((yield self.tagpool.get_metadata("newpool")), {"my": "data"}) @inlineCallbacks def test_purge_pool(self): result = yield self.proxy.callRemote("purge_pool", "pool1") self.assertEqual(result, None) self.assertEqual((yield self.tagpool.free_tags("pool1")), []) @inlineCallbacks def test_purge_pool_with_keys_in_use(self): d = self.proxy.callRemote("purge_pool", "pool2") yield d.addErrback(lambda f: log.err(f)) # txJSON-RPC 0.5 adds support for py3 which means # different error classes are being logged, accept both errors = self.flushLoggedErrors('xmlrpclib.Fault', 'xmlrpc.client.Fault') self.assertEqual(len(errors), 1) server_errors = self.flushLoggedErrors( 'vumi.components.tagpool.TagpoolError') self.assertEqual(len(server_errors), 1) @inlineCallbacks def test_list_pools(self): result = yield self.proxy.callRemote("list_pools") self.assertEqual(sorted(result), ["pool1", "pool2", "pool3"]) @inlineCallbacks def test_free_tags(self): result = yield self.proxy.callRemote("free_tags", "pool1") self.assertEqual(sorted(result), [["pool1", "tag1"], ["pool1", "tag2"]]) result = yield self.proxy.callRemote("free_tags", "pool2") self.assertEqual(result, []) result = yield self.proxy.callRemote("free_tags", "pool3") self.assertEqual(result, []) @inlineCallbacks def test_inuse_tags(self): result = yield self.proxy.callRemote("inuse_tags", "pool1") self.assertEqual(result, []) result = yield self.proxy.callRemote("inuse_tags", "pool2") self.assertEqual(sorted(result), [["pool2", "tag1"], ["pool2", "tag2"]]) result = yield self.proxy.callRemote("inuse_tags", "pool3") self.assertEqual(result, []) @inlineCallbacks def test_acquired_by(self): result = yield self.proxy.callRemote("acquired_by", ["pool1", "tag1"]) self.assertEqual(result, [None, None]) result = yield self.proxy.callRemote("acquired_by", ["pool2", "tag1"]) self._check_reason(result, None, {}) yield self.tagpool.acquire_tag("pool1", owner="me", reason={"foo": "bar"}) result = yield self.proxy.callRemote("acquired_by", ["pool1", "tag1"]) self._check_reason(result, "me", {"foo": "bar"}) @inlineCallbacks def test_owned_tags(self): result = yield self.proxy.callRemote("owned_tags", None) self.assertEqual(sorted(result), [[u'pool2', u'tag1'], [u'pool2', u'tag2']]) yield self.tagpool.acquire_tag("pool1", owner="me", reason={"foo": "bar"}) result = yield self.proxy.callRemote("owned_tags", "me") self.assertEqual(result, [["pool1", "tag1"]])
is_array = True else: if is_array: if (a.startswith('[') and a.endswith(']')) or (a.startswith('{') and a.endswith('}')): args.append(eval(a)) elif not len(a): args.append([]) else: args.append([a]) elif a == "False" or a == "True": args.append(eval(a)) else: try: a = int(a) except: pass args.append(a) is_array = False re_clean_args = re.compile(r"^\[(.*)\]$") if not inline: print "CALL:", command, re_clean_args.sub(r"\1", str(args)) d = proxy.callRemote(command, *args) d.addCallback(printValue).addErrback(printError) d.addCallback(shutdown) reactor.run()
from txjsonrpc.web.jsonrpc import Proxy import sys def printValue(value): print "Result: %s" % str(value) def printError(error): print ' !! ERROR: ', error def shutDown(data): print "Shutting down reactor..." reactor.stop() proxy = Proxy("http://127.0.0.1:8080/") if len(sys.argv) > 2 and sys.argv[2] == "array": print sys.argv[1], [a for a in sys.argv[3:]] d = proxy.callRemote(sys.argv[1], [a for a in sys.argv[3:]]) else: print sys.argv[1], sys.argv[2:] d = proxy.callRemote(sys.argv[1], *sys.argv[2:]) d.addCallback(printValue).addErrback(printError) d.addCallback(shutDown) reactor.run() # usage : # python test_jsonrpc.py echo coucou
def main(): proxy = Proxy('http://127.0.0.1:7080/') c = proxy.callRemote('getRestaurant', "Test") c.addCallback(printValue).addErrback(printError)
def query(cls, ip, port, action, *args): daemon = Proxy('http://%s:%d/' % (ip,port)) return daemon.callRemote(action, *args)
# here we use certificates from txjsonrpc/test/certs/ directory self.ca_cert_file = certs.ca_cert_file() self.cert_file = certs.cert_file('pyclient') self.key_file = certs.key_file('pyclient') def getContext(self): ctx = ssl.ClientContextFactory.getContext(self) ctx.set_verify(SSL.VERIFY_PEER, server_cert_verify_callback) ctx.set_verify_depth(10) ctx.load_verify_locations(self.ca_cert_file) ctx.use_certificate_file(self.cert_file) ctx.use_privatekey_file(self.key_file) return ctx # ------------------------------------------------------------ import sys log.startLogging(sys.stdout) if len(sys.argv) > 1: port = int(sys.argv[1]) else: port = 7443 proxy = Proxy('https://localhost:%d/' % port, ssl_ctx_factory=AltCtxFactory) d = proxy.callRemote('add', 3, 5) d.addCallback(printValue).addErrback(printError).addBoth(shutDown) reactor.run()
class TestTagpoolApiServer(VumiTestCase): @inlineCallbacks def setUp(self): self.persistence_helper = self.add_helper(PersistenceHelper()) self.redis = yield self.persistence_helper.get_redis_manager() self.tagpool = TagpoolManager(self.redis) site = Site(TagpoolApiServer(self.tagpool)) self.server = yield reactor.listenTCP(0, site, interface='127.0.0.1') self.add_cleanup(self.server.loseConnection) addr = self.server.getHost() self.proxy = Proxy("http://%s:%d/" % (addr.host, addr.port)) yield self.setup_tags() @inlineCallbacks def setup_tags(self): # pool1 has two tags which are free yield self.tagpool.declare_tags([ ("pool1", "tag1"), ("pool1", "tag2")]) # pool2 has two tags which are used yield self.tagpool.declare_tags([ ("pool2", "tag1"), ("pool2", "tag2")]) yield self.tagpool.acquire_specific_tag(["pool2", "tag1"]) yield self.tagpool.acquire_specific_tag(["pool2", "tag2"]) # pool3 is empty but has metadata yield self.tagpool.set_metadata("pool3", {"meta": "data"}) def _check_reason(self, result, expected_owner, expected_reason): owner, reason = result self.assertEqual(owner, expected_owner) self.assertEqual(reason.pop('owner'), expected_owner) self.assertTrue(isinstance(reason.pop('timestamp'), float)) self.assertEqual(reason, expected_reason) @inlineCallbacks def test_acquire_tag(self): result = yield self.proxy.callRemote("acquire_tag", "pool1") self.assertEqual(result, ["pool1", "tag1"]) self.assertEqual((yield self.tagpool.inuse_tags("pool1")), [("pool1", "tag1")]) result = yield self.proxy.callRemote("acquire_tag", "pool2") self.assertEqual(result, None) @inlineCallbacks def test_acquire_tag_with_owner_and_reason(self): result = yield self.proxy.callRemote( "acquire_tag", "pool1", "me", {"foo": "bar"}) self.assertEqual(result, ["pool1", "tag1"]) result = yield self.tagpool.acquired_by(["pool1", "tag1"]) self._check_reason(result, "me", {"foo": "bar"}) @inlineCallbacks def test_acquire_specific_tag(self): result = yield self.proxy.callRemote("acquire_specific_tag", ["pool1", "tag1"]) self.assertEqual(result, ["pool1", "tag1"]) self.assertEqual((yield self.tagpool.inuse_tags("pool1")), [("pool1", "tag1")]) result = yield self.proxy.callRemote("acquire_specific_tag", ["pool2", "tag1"]) self.assertEqual(result, None) @inlineCallbacks def test_acquire_specific_tag_with_owner_and_reason(self): result = yield self.proxy.callRemote( "acquire_specific_tag", ["pool1", "tag1"], "me", {"foo": "bar"}) self.assertEqual(result, ["pool1", "tag1"]) result = yield self.tagpool.acquired_by(["pool1", "tag1"]) self._check_reason(result, "me", {"foo": "bar"}) @inlineCallbacks def test_release_tag(self): result = yield self.proxy.callRemote("release_tag", ["pool1", "tag1"]) self.assertEqual(result, None) result = yield self.proxy.callRemote("release_tag", ["pool2", "tag1"]) self.assertEqual(result, None) self.assertEqual((yield self.tagpool.inuse_tags("pool2")), [("pool2", "tag2")]) @inlineCallbacks def test_declare_tags(self): tags = [("newpool", "tag1"), ("newpool", "tag2")] result = yield self.proxy.callRemote("declare_tags", tags) self.assertEqual(result, None) free_tags = yield self.tagpool.free_tags("newpool") self.assertEqual(sorted(free_tags), sorted(tags)) @inlineCallbacks def test_get_metadata(self): result = yield self.proxy.callRemote("get_metadata", "pool3") self.assertEqual(result, {"meta": "data"}) result = yield self.proxy.callRemote("get_metadata", "pool1") self.assertEqual(result, {}) @inlineCallbacks def test_set_metadata(self): result = yield self.proxy.callRemote("set_metadata", "newpool", {"my": "data"}) self.assertEqual(result, None) self.assertEqual((yield self.tagpool.get_metadata("newpool")), {"my": "data"}) @inlineCallbacks def test_purge_pool(self): result = yield self.proxy.callRemote("purge_pool", "pool1") self.assertEqual(result, None) self.assertEqual((yield self.tagpool.free_tags("pool1")), []) @inlineCallbacks def test_purge_pool_with_keys_in_use(self): d = self.proxy.callRemote("purge_pool", "pool2") yield d.addErrback(lambda f: log.err(f)) # txJSON-RPC 0.5 adds support for py3 which means # different error classes are being logged, accept both errors = self.flushLoggedErrors('xmlrpclib.Fault', 'xmlrpc.client.Fault') self.assertEqual(len(errors), 1) server_errors = self.flushLoggedErrors( 'vumi.components.tagpool.TagpoolError') self.assertEqual(len(server_errors), 1) @inlineCallbacks def test_list_pools(self): result = yield self.proxy.callRemote("list_pools") self.assertEqual(sorted(result), ["pool1", "pool2", "pool3"]) @inlineCallbacks def test_free_tags(self): result = yield self.proxy.callRemote("free_tags", "pool1") self.assertEqual( sorted(result), [["pool1", "tag1"], ["pool1", "tag2"]]) result = yield self.proxy.callRemote("free_tags", "pool2") self.assertEqual(result, []) result = yield self.proxy.callRemote("free_tags", "pool3") self.assertEqual(result, []) @inlineCallbacks def test_inuse_tags(self): result = yield self.proxy.callRemote("inuse_tags", "pool1") self.assertEqual(result, []) result = yield self.proxy.callRemote("inuse_tags", "pool2") self.assertEqual( sorted(result), [["pool2", "tag1"], ["pool2", "tag2"]]) result = yield self.proxy.callRemote("inuse_tags", "pool3") self.assertEqual(result, []) @inlineCallbacks def test_acquired_by(self): result = yield self.proxy.callRemote("acquired_by", ["pool1", "tag1"]) self.assertEqual(result, [None, None]) result = yield self.proxy.callRemote("acquired_by", ["pool2", "tag1"]) self._check_reason(result, None, {}) yield self.tagpool.acquire_tag("pool1", owner="me", reason={"foo": "bar"}) result = yield self.proxy.callRemote("acquired_by", ["pool1", "tag1"]) self._check_reason(result, "me", {"foo": "bar"}) @inlineCallbacks def test_owned_tags(self): result = yield self.proxy.callRemote("owned_tags", None) self.assertEqual(sorted(result), [[u'pool2', u'tag1'], [u'pool2', u'tag2']]) yield self.tagpool.acquire_tag("pool1", owner="me", reason={"foo": "bar"}) result = yield self.proxy.callRemote("owned_tags", "me") self.assertEqual(result, [["pool1", "tag1"]])
res = yield proxy.callRemote('start_corpus', cid, environ['HYPHE_ADMIN_PASSWORD'] if corpus['password'] else '') if res['code'] == 'fail': print >> sys.stderr, "WARNING: could not start old corpus %s: %s" % (cid, res['message']) continue res = yield proxy.callRemote('ping', cid, 30) if res['code'] == 'fail': print >> sys.stderr, "WARNING: could not ping old corpus %s: %s" % (cid, res['message']) continue res = yield proxy.callRemote('force_destroy_corpus', cid) if res['code'] == 'fail': print >> sys.stderr, "WARNING: could not destroy old corpus %s: %s" % (cid, res['message']) else: destroyed.append(cid) c = MongoClient(environ["HYPHE_MONGODB_HOST"], int(environ["HYPHE_MONGODB_PORT"])) for d in destroyed: c.drop_database('%s_%s' % (environ["HYPHE_MONGODB_DBNAME"], d)) def printError(error): print >> sys.stderr, "ERROR while working with corpus", curcorpus, error traceback.print_stack(file=sys.stderr) def shutdown(data): reactor.stop() d = proxy.callRemote('list_corpus') d.addCallback(handleList).addErrback(printError) d.addCallback(shutdown) reactor.run()
status, message = error.value.args if status == "401": print message return print 'error', error def shutDown(data): print "Shutting down reactor..." reactor.stop() proxyUnauth = Proxy('http://127.0.0.1:7080/') dl = [] d = proxyUnauth.callRemote('echo', 'bite me') d.addCallbacks(printValue, printError) dl.append(d) d = proxyUnauth.callRemote('add', 3, 5) d.addCallbacks(printValue, printError) dl.append(d) proxyAuth = Proxy('http://*****:*****@127.0.0.1:7080/') d = proxyAuth.callRemote('echo', 'bite me') d.addCallbacks(printValue, printError) dl.append(d) d = proxyAuth.callRemote('add', 3, 5) d.addCallbacks(printValue, printError)
def getWE(dl) : d = proxy.callRemote('getWebEntities') d.addCallbacks(printValue, printError) dl.append(d) d = proxy.callRemote('monitorCrawl') d.addCallbacks(printValue, printError) dl.append(d) watchWE(dl) def watchWE(dl) : d=reactor.callLater(1,getWE,dl) dl.append(d) proxy = Proxy('http://127.0.0.1:8080/') dl = [] watchWE(dl) d = proxy.callRemote('crawl',[urlTokenizer("http://www.sciencespo.fr"),urlTokenizer("http://medialab.sciences-po.fr")]) d.addCallbacks(printValue, printError) dl.append(d) reactor.run()
class ControllerTester(unittest.TestCase): #note: setUp is called right before each test def setUp(self): self.PORT = 7070 self.daemon = Proxy('http://127.0.0.1:%d/' % self.PORT) self.tdef = "../templatedef.xml" self.tins = "../templateins.xml" self.tdef_id = "pkt_counter_aggregator" self.tins_id = "pkt_counter_aggregator" def test_put_template(self): f = open(self.tdef, "r") fxml = f.read() f.close() d = self.daemon.callRemote('put_template',fxml) d.addCallback(print_results).addErrback(print_error) return d def test_remove_template(self): d = self.daemon.callRemote('remove_template',self.tdef_id) d.addCallback(print_results).addErrback(print_error) return d def test_get_templates(self): d = self.daemon.callRemote('get_templates') d.addCallback(print_results).addErrback(print_error) return d def test_expand_template(self): f = open(self.tins, "r") fxml = f.read() f.close() d = self.daemon.callRemote('expand_template', fxml) d.addCallback(print_results).addErrback(print_error) return d def test_invoke_template(self): f = open(self.tins, "r") fxml = f.read() f.close() d = self.daemon.callRemote('invoke_template', fxml) d.addCallback(print_results).addErrback(print_error) return d def test_invoke_template_nodes(self): f = open(self.tins, "r") fxml = f.read() f.close() nodes = [('127.0.0.1',7081,10005),('127.0.0.1',7082,10006)] d = self.daemon.callRemote('invoke_template', fxml, nodes) d.addCallback(print_results).addErrback(print_error) return d def test_stop_template(self): d = self.daemon.callRemote('stop_template',self.tins_id) d.addCallback(print_results).addErrback(print_error) return d def test_read_variable(self): temp_id, comp_id = self.tins_id, "step2" block_id, var_id = "counter", "pktcnt" print temp_id,comp_id,block_id,var_id d = self.daemon.callRemote('get_variable',temp_id, comp_id, block_id, var_id) d.addCallback(print_results).addErrback(print_error) return d def test_write_variable(self): temp_id, comp_id = self.tins_id, "step2" block_id, var_id = "counter", "reset" d = self.daemon.callRemote('write_variable',temp_id, comp_id, block_id, var_id, "1") d.addCallback(print_results).addErrback(print_error) return d def test_get_supported_topologies(self): d = self.daemon.callRemote('get_supported_topologies') d.addCallback(print_results).addErrback(print_error) return d def test_get_supported_blocks(self): d = self.daemon.callRemote('get_supported_blocks') d.addCallback(print_results).addErrback(print_error) return d def test_get_block_infos(self): block_types = ["PFQSource","PacketPrinter"] d = self.daemon.callRemote('get_block_infos', block_types) d.addCallback(print_results).addErrback(print_error) return d def test_save_datafile(self): import base64 f = open("../datafile.zip", "rb") fbin = base64.b64encode(f.read()) f.close() d = self.daemon.callRemote('save_datafile', 'test.zip',fbin) d.addCallback(print_results).addErrback(print_error) return d test_put_template.skip = "disabled locally" #test_remove_template.skip = "disabled locally" test_expand_template.skip = "disabled locally" test_invoke_template.skip = "disabled locally" test_invoke_template_nodes.skip = "disabled locally" test_stop_template.skip = "disabled locally" #test_read_variable.skip = "disabled locally" test_write_variable.skip = "disabled locally" test_get_supported_blocks.skip = "disabled locally" test_get_block_infos.skip = "disabled locally" test_get_supported_topologies.skip = "disabled locally" test_get_templates.skip = "disabled locally" test_save_datafile.skip = "disabled locally"
print "REMOVING old corpus:", cid, corpus['last_activity'], int(since/dayms), "days old" res = yield proxy.callRemote('start_corpus', cid, config['ADMIN_PASSWORD'] if corpus['password'] else '') if res['code'] == 'fail': print >> sys.stderr, "WARNING: could not start old corpus %s: %s" % (cid, res['message']) continue res = yield proxy.callRemote('ping', cid, 30) if res['code'] == 'fail': print >> sys.stderr, "WARNING: could not ping old corpus %s: %s" % (cid, res['message']) continue res = yield proxy.callRemote('destroy_corpus', cid) if res['code'] == 'fail': print sys.stderr, "WARNING: could not destroy old corpus %s: %s" % (cid, res['message']) else: destroyed.append(cid) c = MongoClient(config["mongo-scrapy"]["host"], config["mongo-scrapy"]["mongo_port"]) for d in destroyed: c.drop_database('%s_%s' % (config["mongo-scrapy"]["db_name"], d)) def printError(error): print >> sys.stderr, "ERROR: Cannot get list of corpora", error def shutdown(data): reactor.stop() d = proxy.callRemote('list_corpus') d.addCallback(handleList).addErrback(printError) d.addCallback(shutdown) reactor.run()
def shut_down(data): print "Shutting down reactor..." reactor.stop() def shut_down_harsh(data): print "Shutting down reactor..." os._exit(1) bm_daemon = Proxy('http://127.0.0.1:' + str(DAEMON_PORT) + '/') value = int(sys.argv[1]) if value == 1: d = bm_daemon.callRemote('get_blocks_list') d.addCallback(print_results).addErrback(print_error).addBoth(shut_down) elif value == 2: d = bm_daemon.callRemote('get_hw_specs') d.addCallback(print_results).addErrback(print_error).addBoth(shut_down) elif value == 3: b = [ 'SketchMerger', 'L4Demux', 'PcapSource', 'ComboSZE2Source', 'PerFlowStats', 'PktPairifier', 'Null', 'SynthSource', 'CDFGenerator', 'SynFloodDetector', 'SketchFlowCounter', 'PacketPrinter', 'TCPFlagCounter', 'PktCounter', 'IPFIXExporter', 'RRDemux', 'TopNFlowSelector', 'PFQSource', 'IpDumbAnonymizer', 'IPFIXSource', 'PFRingSource' ]
class ShutterJSONRPCTestCase(unittest.TestCase): def setUp(self): self.port = reactor.listenTCP( 8001, RemoteShutterFactory(ShutterJSONRPCProtocol())) self.client = Proxy('http://127.0.0.1:8001', version=jsonrpclib.VERSION_2) self.conn = psycopg2.connect(config.DB_CONN) self.cur = self.conn.cursor() if not os.path.exists("static"): os.makedirs("static") def tearDown(self): self.cur.execute("TRUNCATE TABLE shutter.urls CASCADE") self.conn.commit() self.port.stopListening() self.conn.close() for target in os.listdir("static"): os.unlink("static/%s" % target) os.rmdir("static") def test_snapshots_empty(self): def _result(value): self.assertTrue(value['error'] is None) self.assertFalse(value['result'] is None) self.assertEqual(value['result'], []) return self.client.callRemote( "snapshots", "http://programistamag.pl").addCallback(_result) def test_snapshots_fake_entries(self): def _result(value): # Check the return values self.assertEqual(value['result'], ["fake3.png", "fake2.png", "fake1.png"]) # Add fake entries into the database url = "http://fake.test.com" self.cur.execute( """INSERT INTO shutter.urls(url) VALUES (%s) RETURNING id """, [url]) url_id = self.cur.fetchone()[0] def _make_file(fakeFile): self.cur.execute( """INSERT INTO shutter.snapshots(url_id, file_path) VALUES (%s, %s)""", [url_id, fakeFile]) self.conn.commit() _make_file("fake1.png") _make_file("fake2.png") _make_file("fake3.png") return self.client.callRemote( "snapshots", "http://fake.test.com").addCallback(_result) def test_snapshot(self): def _result(value): self.assertTrue(value['error'] is None) self.assertFalse(value['result'] is None) # The file name is 36 characters long, 32 for the checksum # and 4 characters for the extension and dot. self.assertEqual(len(value['result']), 36) return self.client.callRemote( "snapshot", "http://programistamag.pl").addCallback(_result)
args.append(eval(a)) elif not len(a): print('not len') args.append([]) else: print('append [a]') args.append([a]) elif a == "False" or a == "True": print('boolean weird convert') args.append(eval(a)) else: if auto_convert_integers: try: a = int(a) except: pass print('append a') args.append(a) is_array = False display_message('call args') print(args) re_clean_args = re.compile(r"^\[(.*)\]$") if not inline: print("CALL:", command, re_clean_args.sub(r"\1", str(args))) d = proxy.callRemote(command, *args) d.addCallback(printValue).addErrback(printError) d.addCallback(shutdown) reactor.run()
print "=========================" print "start: installs a composition" print "read: reads a set of variables from a block" print "stop: uninstalls a composition" os._exit(1) op = sys.argv[1] if op == "start": if len(sys.argv) < 3: print "usage: daemonclient.py start [composition]" os._exit(1) f = open(sys.argv[2], "r") c = f.read() f.close() d = bm_daemon.callRemote('start_composition', c) d.addCallback(print_results).addErrback(print_error).addBoth(shut_down) elif op == "stop": if len(sys.argv) < 3: print "usage: daemonclient.py stop [composition id]" os._exit(1) d = bm_daemon.callRemote('stop_composition', sys.argv[2]) d.addCallback(print_results).addErrback(print_error).addBoth(shut_down) elif op == "read": if len(sys.argv) < 5: print "usage: daemonclient.py read [composition id] [blockname] [variables...]" print "example: daemonclient.py read mycomp counter pktcnt bytecnt" os._exit(1)
from twisted.internet import reactor from txjsonrpc.web.jsonrpc import Proxy def printValue(value): print "Result: %s" % str(value) reactor.stop() def printError(error): print 'error', error reactor.stop() proxy = Proxy('http://127.0.0.1:7080/') proxy.callRemote('add', 3, 5).addCallbacks(printValue, printError) reactor.run()