A flexible Python implementation of the JSON-RPC 2.0 protocol.
Project description
pyjsonrpc2
A flexible Python implementation of the JSON-RPC 2.0 protocol (currently server-side only).
Key features
- Full compliance with the JSON-RPC 2.0 specification
- No transport functionality
- Multiple method registration patterns (class-based, individual methods, lambda, etc.)
- Automatic & custom error handling capabilities
- Support for both string and bytes input
- Complete type hints (passes
mypy --strict) - Extensive unit tests (full coverage)
- Semantic versioning adherence
Installation
To install the package, use pip:
pip install pyjsonrpc2
Usage
For more info, check the /examples directory.
Basic Server Creation
from pyjsonrpc2.server import JsonRpcServer, rpc_method, JsonRpcError
# Create a basic server
server = JsonRpcServer()
Method Registration Patterns
These are the main patterns for registering RPC methods. /examples/registering_methods.py contains a few more.
- Class-based approach with decorators:
class MathServer(JsonRpcServer):
@rpc_method
def square(self, x):
return x**2
@rpc_method(name="cube")
def calculate_cube(self, x):
return x**3
server = MathServer()
- Adding individual methods using decorators:
@server.add_method
def add(a, b):
return a + b
- Adding methods with custom names:
def sub(a, b):
return a - b
server.add_method(sub, name="substract")
- Adding lambda functions:
server.add_method(lambda a, b: a % b, name="modulo")
Error Handling
Error handling features:
- Custom error codes for implementation-defined & application-defined errors through the
JsonRpcErrorclass - Automatic conversion of Python exceptions to JSON-RPC Internal error responses
- Support for additional error data in a structured format
- Built-in handling of protocol-level errors (invalid JSON, missing required fields, etc.)
- Error logging for debugging purposes
- Custom Implementation-Defined Errors:
class AdvancedMathServer(JsonRpcServer):
@rpc_method
def divide(self, a, b):
if b == 0:
raise JsonRpcError(
code=-32000,
message="Division by zero",
data={"numerator": a, "denominator": b}
)
return a / b
- Multiple Error Conditions:
@rpc_method
def factorial(self, n):
if not isinstance(n, int):
# Regular exceptions are caught and converted to Internal error responses
raise TypeError("n must be an integer")
if n < 0:
# Custom JSON-RPC errors with additional data
raise JsonRpcError(
code=-32001,
message="Invalid input for factorial",
data={"input": n, "reason": "Must be non-negative"}
)
# ... implementation ...
Request execution
result = server.call('{"jsonrpc": "2.0", "method": "add", "params": [5, 3], "id": 1}')
result = server.call(b'{"jsonrpc": "2.0", "method": "subtract", "params": [5, 3], "id": 2}')
Tests
The simplest way to run tests is:
python -m unittest
As a more robust alternative, you can install tox to automatically test across the supported python versions, then run:
tox -p
Issue tracker
Please report any bugs or enhancement ideas using the issue tracker.
License
pyjsonrpc2 is licensed under the terms of the MIT License.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pyjsonrpc2-1.0.1.tar.gz.
File metadata
- Download URL: pyjsonrpc2-1.0.1.tar.gz
- Upload date:
- Size: 14.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.5.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ecc90eb901efde01d250c7c43fcd88af0f2571e8e45df39562603727b781ce93
|
|
| MD5 |
ddda48a6ebc5f203f7e954bbb0acf413
|
|
| BLAKE2b-256 |
f99e8c63d9ab8a4b87121ef66bf55a1bbb2a330f454d5fb8c6daafa6bed23a6a
|
File details
Details for the file pyjsonrpc2-1.0.1-py3-none-any.whl.
File metadata
- Download URL: pyjsonrpc2-1.0.1-py3-none-any.whl
- Upload date:
- Size: 7.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.5.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bf11538fd99febd38be3b3992630dd04a06bf70d7dd73c5f274099db8ddd51ff
|
|
| MD5 |
a5cd6cd27f7410184d1476e3097242ed
|
|
| BLAKE2b-256 |
6b4213fdd81a190d082c070c3517422658e8f6b3edc7e9a93ba82b0e2c5cbf1a
|