jsonrpcclient

jsonrpcclient Examples

Showing how to send JSON-RPC requests using various frameworks and transport protocols.

Requests

HTTPClient uses the Requests library.

$ pip install jsonrpcclient requests
>>> from jsonrpcclient.http_client import HTTPClient
>>> HTTPClient('http://localhost:5000/').request('ping')
--> {"jsonrpc": "2.0", "method": "ping", "id": 1}
<-- {"jsonrpc": "2.0", "result": "pong", "id": 1}
'pong'

Tornado

TornadoClient uses Tornado to send an asynchronous request.

$ pip install jsonrpcclient tornado
from tornado import ioloop
from jsonrpcclient.tornado_client import TornadoClient

client = TornadoClient('http://localhost:5000/')

async def main():
    result = await client.request('ping')
    print(result)

ioloop.IOLoop.current().run_sync(main)

Note the async/await syntax requires Python 3.5+. Prior to that use @gen.coroutine and yield.

$ python client.py
INFO:jsonrpcclient.client.request:{"jsonrpc": "2.0", "method": "ping", "id": 1}
INFO:jsonrpcclient.client.response:{"jsonrpc": "2.0", "result": "pong", "id": 1}
pong

See blog post.

ZeroMQ

ZMQClient uses pyzmq for comms with a ZeroMQ server.

$ pip install jsonrpcclient pyzmq
>>> from jsonrpcclient.zmq_client import ZMQClient
>>> ZMQClient('tcp://localhost:5000').request('ping')
--> {"jsonrpc": "2.0", "method": "ping", "id": 1}
<-- {"jsonrpc": "2.0", "result": "pong", "id": 1}
'pong'

See blog post.