jsonrpcclient Examples¶
Showing how to send JSON-RPC requests using various frameworks and transport protocols.
aiohttp¶
aiohttpClient
uses the aiohttp
library:
$ 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))
Requests¶
HTTPClient
uses the Requests library.
$ pip install 'jsonrpcclient[requests]'
from jsonrpcclient.http_client import HTTPClient
response = HTTPClient('http://localhost:5000/').request('ping')
print(response)
Tornado¶
TornadoClient
uses Tornado to send an
asynchronous request.
$ 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)
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.
Websockets¶
WebSocketsClient
uses the websockets
library:
$ 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))