.. rubric:: :doc:`index`
jsonrpcclient Examples
**********************
Showing how to send JSON-RPC requests using various frameworks and transport
protocols.
.. contents::
:local:
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 `__.