jsonrpcclient¶
Create JSON-RPC 2.0 requests and parse responses in Python.
jsonrpcclient builds JSON-RPC 2.0 requests and parses the responses. It doesn't send anything, so it works with any transport: HTTP, websockets, ZeroMQ, or something of your own. It has no dependencies, ships type hints, and is safe to use from several threads. It supports Python 3.8 to 3.14, including free-threaded 3.14t.
Install¶
pip install jsonrpcclient
Quickstart¶
This sends ping to a JSON-RPC server at http://localhost:8000/, using
requests for the HTTP part:
import requests
from jsonrpcclient import Error, Ok, parse, request
url = "http://localhost:8000/"
response = requests.post(url, json=request("ping"), timeout=10)
response.raise_for_status()
parsed = parse(response.json())
if isinstance(parsed, Ok):
print(parsed.result)
elif isinstance(parsed, Error):
print("Error:", parsed.message)
Output
pong
request builds the request as a dict, and parse turns the reply into an
Ok or an Error:
>>> from jsonrpcclient import parse, request
>>> request("ping")
{'jsonrpc': '2.0', 'method': 'ping', 'id': 1}
>>> parse({"jsonrpc": "2.0", "result": "pong", "id": 1})
Ok(result='pong', id=1)
For JSON strings, use request_json and parse_json.
Where next¶
- Requests, notifications, batches and ids: building what you send.
- Responses and error handling: reading what comes back, and every way it can go wrong.
- Transports: complete examples with urllib, requests, httpx, aiohttp, websockets and ZeroMQ.
- API reference: every function and class, with signatures.
- Typing and threads and async.
- Migrating from 3.x: the 4.x API is completely different.
- jsonrpcserver: the same idea for the server side.