Python Twisted JSON RPC Python Twisted JSON RPC python python

Python Twisted JSON RPC


txJSONRPC is great. I use it and it works. I suggest you give it a try.

SERVER:

from txjsonrpc.web import jsonrpcfrom twisted.web import serverfrom twisted.internet import reactorclass Math(jsonrpc.JSONRPC):    """    An example object to be published.    """    def jsonrpc_add(self, a, b):        """        Return sum of arguments.        """        return a + breactor.listenTCP(7080, server.Site(Math()))reactor.run()

CLIENT:

from twisted.internet import reactorfrom txjsonrpc.web.jsonrpc import Proxydef printValue(value):    print "Result: %s" % str(value)def printError(error):    print 'error', errordef shutDown(data):    print "Shutting down reactor..."    reactor.stop()proxy = Proxy('http://127.0.0.1:7080/')d = proxy.callRemote('add', 3, 5)d.addCallback(printValue).addErrback(printError).addBoth(shutDown)reactor.run()

As a bonus, I will leave some alternative: amp.http://amp-protocol.net


If you are looking for a framework-independent approach, this lib I pushed (using mixin) might be helpful:


Cyclone, a Tornado async web server implementation written using twisted, has a built-in json-rpc request handler that uses the python json/simplejson module. Example server and client code is here.