Minimal python RPC implementation based on the JSON-RPC 2.0 specs.
Original source hosted at GitHub.
Usage
jsonrpyc.RPC instances basically wrap an input stream and an output stream in order to communicate with other services.
A service is not even forced to be written in Python as long as it strictly implements the JSON-RPC 2.0 specs.
A suitable implementation for NodeJs is node-json-rpc.
A jsonrpyc.RPC instance may wrap a target object.
Incomming requests will be routed to methods of this object whose result might be sent back as a response. Example implementation:
server.py
import jsonrpyc
class MyTarget:
def greet(self, name: str) -> str:
return f"Hi, {name}!"
jsonrpyc.RPC(MyTarget())
client.py
import jsonrpyc
from subprocess import Popen, PIPE
p = Popen(["python", "server.py"], stdin=PIPE, stdout=PIPE)
rpc = jsonrpyc.RPC(stdout=p.stdin, stdin=p.stdout)
#
# sync usage
#
print(rpc("greet", args=("John",), block=0.1))
# => "Hi, John!"
#
# async usage
#
def cb(err: Exception | None, res: str | None = None) -> None:
if err:
raise err
print(f"callback got: {res}")
rpc("greet", args=("John",), callback=cb)
# cb is called asynchronously which prints
# => "callback got: Hi, John!"
#
# shutdown
#
p.stdin.close()
p.stdout.close()
p.terminate()
p.wait()
Installation
Install simply via pip.
pip install jsonrpyc
# or with optional dev dependencies
pip install jsonrpyc[dev]
Contributing
If you like to contribute to jsonrpyc, I'm happy to receive pull requests.
The full testing pipeline is based on pre-commit. Run the following to install development dependencies and set it up:
# inside the cloned repository
git lfs install
pip install -e .[dev]
pre-commit install
Now, every time you make a commit, the pre-commit and pre-push hooks will automatically run linting, type checking and unit tests. To run them manually, use
# for linting, type checking and additional checks on all files
pre-commit run --all-files
# only for staged files
pre-commit run
# or run unit tests standalone
pytest
Development
- Source hosted at GitHub
- Report issues, questions, feature requests on GitHub Issues
Metadata
Release files for jsonrpyc 1.4.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| jsonrpyc-1.4.0.tar.gz | 12.0 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| jsonrpyc-1.4.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 23.5 kB
Release files / jsonrpyc-1.4.0.tar.gz
| Download URL | jsonrpyc-1.4.0.tar.gz |
|---|---|
| Size | 12.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
d3121255d7ca804ee8c3a8ad361d38e47a528ac690071f41674d975801681dcc
|
|
BLAKE2b-256 checksum How to use checksums |
a190ef988d3064eee1bac6240f0962a13e173b0e262f92ee4a859fa7ab4a42f0
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Release files / jsonrpyc-1.4.0-py3-none-any.whl
| Download URL | jsonrpyc-1.4.0-py3-none-any.whl |
|---|---|
| Size | 11.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
1021ef9a489f1794a2e300df9213e7fd49c5b9656ac2631f50eb90ad3eecbfe2
|
|
BLAKE2b-256 checksum How to use checksums |
a1f08632bbd7c4fe23b2d7a4bdd0d59193c754c57076623357455780bac0ca0b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|