Exemplo n.º 1
0
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)
Exemplo n.º 2
0
    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)))
Exemplo n.º 3
0
 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)
Exemplo n.º 4
0
    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)
Exemplo n.º 5
0
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)
Exemplo n.º 6
0
	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"
Exemplo n.º 7
0
    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")
Exemplo n.º 8
0
 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()
Exemplo n.º 9
0
 def __init__(self, host, port, json_callback, backout_callback, usessl):
     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) + "/")
Exemplo n.º 10
0
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)
Exemplo n.º 11
0
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)
Exemplo n.º 12
0
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"
Exemplo n.º 13
0
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)
Exemplo n.º 14
0
def main():
    print("\033[94mJSON-RPC client started\033[0m")

    # Server connection data we want to connect
    serverIP = "localhost"
    port = "12345"
    # Enables the Twisted debug mode, if needed
    debugmode = False

    # Creates the JSONRPC Connection with our context factory to
    # authenticate us against the server with our private key and ensure a
    # strong encryption method as well as setting our trusted CA for our
    # server connection.
    proxy = Proxy('https://%s:%s' % (serverIP,
                                     port),
                  ssl_ctx_factory=AltCtxFactory)

    # Calling function "echo" at server "proxy", using printValue if
    # everything went fine, using printError if somethging went wrong
    # as callback function.
    # Passing the message "Servertest passed" to the RPC Server, which returns
    # that value if erverything went fine.
    callRemote(proxy, "echo", printValue, printError, "Servertest passed")
    # Calling function "multiply" at server "proxy", using printValue if
    # everything went fine, using printError if somethging went wrong
    # as callback function.
    # Passing the integers to the RPC Server, which returns
    # the result 42 if erverything went fine.
    callRemote(proxy, "multiply", printMultiplyResult, printError, 6, 7)
    # Enable the Twisted logging module if defined
    if(debugmode):
        from twisted.python import log
        log.startLogging(sys.stdout)
    # Starting our Client jobs
    reactor.run()
Exemplo n.º 15
0
    def get_api_worker(self, config=None, start=True, auth=True):
        config = {} if config is None else config
        config.setdefault('worker_name', 'test_api_worker')
        config.setdefault('twisted_endpoint', 'tcp:0')
        config.setdefault('web_path', 'api')
        config.setdefault('health_path', 'health')
        config = self.vumi_helper.mk_config(config)
        worker = yield self.vumi_helper.get_worker_helper().get_worker(
            GoApiWorker, config, start)

        vumi_api = worker.vumi_api
        self.vumi_helper.set_vumi_api(vumi_api)
        user, password = None, None
        if auth:
            user_helper = yield self.vumi_helper.make_user(u"user-1")
            session_id = "session-1"
            session = {}
            vumi_api.session_manager.set_user_account_key(
                session, user_helper.account_key)
            yield vumi_api.session_manager.create_session(session_id,
                                                          session,
                                                          expire_seconds=30)
            user, password = "******", session_id

        if not start:
            returnValue(worker)
        yield worker.startService()

        port = worker._web_service._waitingForPort.result
        addr = port.getHost()

        proxy = Proxy("http://%s:%d/api/" % (addr.host, addr.port),
                      user=user,
                      password=password)
        returnValue((worker, proxy))
Exemplo n.º 16
0
    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))
Exemplo n.º 17
0
    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")
Exemplo n.º 18
0
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()
Exemplo n.º 19
0
    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))
Exemplo n.º 20
0
    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)
Exemplo n.º 21
0
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()
Exemplo n.º 22
0
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()
Exemplo n.º 23
0
 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()
Exemplo n.º 24
0
    def setUp(self):
        self.vumi_helper = yield self.add_helper(VumiApiHelper())
        self.user_helper = yield self.vumi_helper.make_user(u'user')
        self.user_api = self.user_helper.user_api
        self.campaign_key = self.user_helper.account_key

        site = Site(
            GoApiServer(self.campaign_key, self.vumi_helper.get_vumi_api()))
        self.server = yield reactor.listenTCP(0, site)
        self.add_cleanup(self.server.loseConnection)
        addr = self.server.getHost()
        self.proxy = Proxy("http://%s:%d/" % (addr.host, addr.port))
Exemplo n.º 25
0
 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)
Exemplo n.º 26
0
    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))
Exemplo n.º 27
0
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
Exemplo n.º 28
0
 def get_api_worker(self, config=None, start=True):
     config = {} if config is None else config
     config.setdefault('worker_name', 'test_api_worker')
     config.setdefault('twisted_endpoint', 'tcp:0')
     config.setdefault('web_path', 'api')
     config.setdefault('health_path', 'health')
     config = self.persistence_helper.mk_config(config)
     worker = yield self.worker_helper.get_worker(TagpoolApiWorker, config,
                                                  start)
     self.add_cleanup(self.cleanup_worker, worker)
     if not start:
         returnValue(worker)
     yield worker.startService()
     port = worker.services[0]._waitingForPort.result
     addr = port.getHost()
     proxy = Proxy("http://%s:%d/api/" % (addr.host, addr.port))
     returnValue((worker, proxy))
Exemplo n.º 29
0
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
Exemplo n.º 30
0
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
Exemplo n.º 31
0
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)

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()
Exemplo n.º 33
0
config = config_hci.load_config()
if not config:
    exit()

daysback = 7
if len(sys.argv) > 1:
    try:
        daysback = int(sys.argv[1])
    except:
        print >> sys.stderr, "ERROR: argument must be an integer (number of days back)"
        exit(1)
dayms = 1000 * 60 * 60 * 24
delay = dayms * daysback

proxy = Proxy('http://127.0.0.1:%d' % config['core_api_port'])

@defer.inlineCallbacks
def handleList(res):
    if res['code'] == 'fail':
        defer.returnValue(printError(res['message']))
    destroyed = []
    for cid, corpus in res['result'].items():
        since = time() * 1000 - corpus['last_activity']
        if since > delay:
            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)
def main():
    proxy = Proxy('http://127.0.0.1:7080/')
    c = proxy.callRemote('getRestaurant', "Test")
    c.addCallback(printValue).addErrback(printError)
Exemplo n.º 35
0

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)

d = proxy.callRemote('science.compare', 5, 7)
d.addCallbacks(printValue, printError)
dl.append(d)

d = proxy.callRemote('science.compare', 7, 5)
Exemplo n.º 36
0
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()

Exemplo n.º 37
0
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()
Exemplo n.º 38
0
config = config_hci.load_config()
if not config:
    exit()

daysback = 7
if len(sys.argv) > 1:
    try:
        daysback = int(sys.argv[1])
    except:
        print >> sys.stderr, "ERROR: argument must be an integer (number of days back)"
        exit(1)
dayms = 1000 * 60 * 60 * 24
delay = dayms * daysback

proxy = Proxy('http://127.0.0.1:%d' % config['twisted.port'])


@defer.inlineCallbacks
def handleList(res):
    if res['code'] == 'fail':
        defer.returnValue(printError(res['message']))
    for cid, corpus in res['result'].items():
        since = time() * 1000 - corpus['last_activity']
        if since > delay:
            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':
Exemplo n.º 39
0
from pymongo import MongoClient
from traceback import print_stack
from twisted.internet import reactor, defer
from txjsonrpc.web.jsonrpc import Proxy

daysback = 7
if len(sys.argv) > 1:
    try:
        daysback = int(sys.argv[1])
    except:
        print >> sys.stderr, "ERROR: argument must be an integer (number of days back)"
        exit(1)
dayms = 1000 * 60 * 60 * 24
delay = dayms * daysback

proxy = Proxy(environ['HYPHE_API_URL'])
curcorpus = None

@defer.inlineCallbacks
def handleList(res):
    if res['code'] == 'fail':
        defer.returnValue(printError(res['message']))
    destroyed = []
    for cid, corpus in res['result'].items():
        curcorpus = cid
        since = time() * 1000 - corpus['last_activity']
        if since > delay:
            print "REMOVING old corpus:", cid, corpus['last_activity'], int(since/dayms), "days old"
            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'])
Exemplo n.º 40
0
    total = 0

    for v in r.get_value():
        var = v.get_value()
        print str(var)


def print_error(error):
    print 'error', error


def shut_down(data):
    reactor.stop()


bm_daemon = Proxy('http://127.0.0.1:' + str(DAEMON_PORT) + '/')

if len(sys.argv) < 2:
    print "usage: daemonclient.py [operation] [params]"
    print "operations:"
    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]"
Exemplo n.º 41
0
def printValue(value):
    if inline == "json":
        print json.dumps(value)
    elif inline:
        print repr(value).decode("unicode-escape").encode('utf-8')
    else:
        import pprint
        pprint.pprint(value)

def printError(error):
    print ' !! ERROR: ', error

def shutdown(data):
    reactor.stop()

proxy = Proxy('http://127.0.0.1:%d' % config['core_api_port'])
command = sys.argv[startargs - 1]
args = []
is_array = False
for a in sys.argv[startargs:]:
    if a == "array":
        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":
Exemplo n.º 42
0
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"
Exemplo n.º 43
0
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
Exemplo n.º 44
0
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"]])
Exemplo n.º 45
0
	def query(cls, ip, port, action, *args):
		daemon = Proxy('http://%s:%d/' % (ip,port))
		return daemon.callRemote(action, *args)
Exemplo n.º 46
0
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"]])
Exemplo n.º 47
0
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()

Exemplo n.º 48
0
        print str(block)

def print_error(error):
    print 'error', error

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)
Exemplo n.º 49
0
    if inline:
        print repr(value).decode("unicode-escape").encode('utf-8')
    else:
        import pprint
        pprint.pprint(value)


def printError(error):
    print ' !! ERROR: ', error


def shutdown(data):
    reactor.stop()


proxy = Proxy('http://127.0.0.1:%d' % config['twisted']['port'])
command = sys.argv[startargs - 1]
args = []
is_array = False
for a in sys.argv[startargs:]:
    if a == "array":
        is_array = True
    else:
        if is_array:
            if a.startswith('[') and a.endswith(']'):
                args.append(eval(a))
            elif not len(a):
                args.append([])
            else:
                args.append([a])
        else:
Exemplo n.º 50
0

def printError(error):
    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)
Exemplo n.º 51
0
    auto_convert_integers = False

if sys.argv[1] == "inline":
    inline = True
    startargs = 3
elif sys.argv[1] == "json":
    inline = "json"
    startargs = 3
else:
    inline = False
    startargs = 2

print('inline',inline,'startargs',startargs)


proxy = Proxy('http://127.0.0.1:6978')
command = sys.argv[startargs - 1]
args = []
is_array = False
for a in sys.argv[startargs:]:
    display_message(a)
    if a == "array":
        print('isarray')
        is_array = True
    else:
        print('else')
        if is_array:
            print('isarray')
            if (a.startswith('[') and a.endswith(']')) or (a.startswith('{') and a.endswith('}')):
                print('startswith')
                args.append(eval(a))
Exemplo n.º 52
0

def printError(error):
    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)
Exemplo n.º 53
0
def print_readvar_results(value):
    r = jsonpickle.decode(value)
    total = 0

    for v in r.get_value():
        var = v.get_value()
        print str(var)

def print_error(error):
    print 'error', error

def shut_down(data):
    reactor.stop()


bm_daemon = Proxy('http://127.0.0.1:' + str(DAEMON_PORT) + '/')

if len(sys.argv) < 2:
    print "usage: daemonclient.py [operation] [params]"
    print "operations:"
    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]"
Exemplo n.º 54
0
import config_hci, lru

config = config_hci.load_config()
if not config:
    exit()


def printValue(value):
    pprint.pprint(value)


def printError(error):
    print ' !! ERROR: ', error


def shutDown(data):
    #    print " -> Shutting down reactor..."
    reactor.stop()


proxy = Proxy('http://127.0.0.1:%d' % config['twisted']['port'])
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()