jsonrpcclient

jsonrpcclient Examples

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

Synchronous

Requests

$ pip install 'jsonrpcclient[requests]'
from jsonrpcclient.http_client import HTTPClient

response = HTTPClient('http://localhost:5000/').request('ping')
print(response)

ZeroMQ

$ pip install 'jsonrpcclient[pyzmq]'
from jsonrpcclient.zeromq_client import ZeroMQClient

response = ZeroMQClient('tcp://localhost:5000').request('ping')
print(response)

See blog post.

Asynchronous

These require Python 3.5+.

aiohttp

$ pip install 'jsonrpcclient[aiohttp]'
import aiohttp
import asyncio
from jsonrpcclient.aiohttp_client import aiohttpClient

async def main(loop):
    async with aiohttp.ClientSession(loop=loop) as session:
        client = aiohttpClient(session, 'http://localhost:5000/')
        response = await client.request('ping')
        print(response)

loop = asyncio.get_event_loop()
loop.run_until_complete(main(loop))

See blog post.

Tornado

$ pip install 'jsonrpcclient[tornado]'
from tornado.ioloop import IOLoop
from jsonrpcclient.tornado_client import TornadoClient

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

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

IOLoop.current().run_sync(main)

See blog post.

Websockets

$ pip install 'jsonrpcclient[websockets]'
import asyncio
import websockets
from jsonrpcclient.websockets_client import WebSocketsClient

async def main():
    async with websockets.connect('ws://localhost:5000') as ws:
        response = await WebSocketsClient(ws).request('ping')
        print(response)

asyncio.get_event_loop().run_until_complete(main())

See blog post.

ZeroMQ (async)

$ pip install 'jsonrpcclient[pyzmq]'
import asyncio
import zmq
from jsonrpcclient.zeromq_async_client import ZeroMQAsyncClient

async def main():
    client = ZeroMQAsyncClient('tcp://localhost:5000')
    response = await client.request('ping')
    print(response)

asyncio.set_event_loop(zmq.asyncio.ZMQEventLoop())
asyncio.get_event_loop().run_until_complete(main())

See blog post.