19

Can anyone recommend some simple code to set up a simple JSON RPC client and server using twisted?

I found txJSON-RPC, but I was wondering if someone had some experience using some of these anc could recommend something.

Alex Amato
  • 1,591
  • 4
  • 19
  • 32

5 Answers5

21

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

SERVER:

from txjsonrpc.web import jsonrpc
from twisted.web import server
from twisted.internet import reactor

class Math(jsonrpc.JSONRPC):
    """
    An example object to be published.
    """
    def jsonrpc_add(self, a, b):
        """
        Return sum of arguments.
        """
        return a + b

reactor.listenTCP(7080, server.Site(Math()))
reactor.run()

CLIENT:

from twisted.internet import reactor
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: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

nosklo
  • 217,122
  • 57
  • 293
  • 297
  • Worked fine except for two minor changes. I needed to change class Math(jsonrpc.jsonRPC): to class Math(jsonrpc.JSONRPC): in the server and the ports mismatch in the client and server code, have them agree on one and it works fine – Alex Amato Jan 19 '11 at 22:31
  • It might be better to use `d.addCallback(printValue); d.addErrback(printError)` instead of `d.addCallbacks(printValue, printError)` in order to catch errors from `printValue()`. – jfs Jan 20 '11 at 05:04
  • @1337Rooster, @J.F. Sebastian: I've fixed the issues you guys mentioned for future readers. Thanks for the comments. – nosklo Jan 20 '11 at 15:55
  • How do i add extra headers to the request like csrf tokens? For example Transmission's rpc server requires X-Transmission-Session-Id to be appended to the request header. – zer0c00l Apr 04 '11 at 14:49
  • JSON-RPC 2.0 is not supported yet by txjsonrpc. took me a while to figure out . – Sergey Feb 24 '16 at 09:13
2

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

Toon Krijthe
  • 52,876
  • 38
  • 145
  • 202
dmora
  • 21
  • 1
0

wikipedia has a bunch of implementations listed for python: https://en.wikipedia.org/wiki/JSON-RPC#Implementations

That said, txjason feels like the one best integrated with twisted. It seems to support out of order responses out of the box for example. Most of it would be portable to python3 using six. The most horrible part is the parameter validation, which is not exposed in the normal public API anyway.

Elrond
  • 901
  • 9
  • 23
0

For me this worked better then "libraries" , speaking of client.

    TESTDATA = {'id': 1234,
                'method': 'getbalance',
                }
    URL = 'http://localhost:7777'

    d= getPage(URL,method="POST",postdata=json.dumps(TESTDATA))
    d.addBoth(lambda x :print(json.loads(x)))
Sergey
  • 749
  • 6
  • 13
0

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.

mikewaters
  • 3,668
  • 3
  • 28
  • 22