jsonrpcclient Examples¶
Showing how to send JSON-RPC requests using various frameworks and transport protocols.
Synchronous¶
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)
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 (asynchronous)¶
$ 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.