Skip to main content

jsonrpyc logo

Documentation status CI Python version Package version License

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.

Request parameters can be passed as a plain list (positional arguments) or object (keyword arguments), or in the {"args": [...], "kwargs": {...}} format used by jsonrpyc itself.

A jsonrpyc.RPC instance may wrap a target object. Incoming requests will be routed to methods of this object whose result might be sent back as a response. Nested attributes can be addressed via dots (e.g. "sub.method"), while attributes starting with two underscores are not accessible. 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
#

rpc.close()
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

Metadata

Release files for jsonrpyc 1.4.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for jsonrpyc 1.4.1
File Size Uploaded
jsonrpyc-1.4.1.tar.gz 13.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for jsonrpyc 1.4.1
File Interpreter ABI Platform
jsonrpyc-1.4.1-py3-none-any.whl Python 3 none any Details

Total release size: 27.3 kB

Release files / jsonrpyc-1.4.1.tar.gz

Download URL jsonrpyc-1.4.1.tar.gz
Size 13.8 kB
Tags Source
SHA-256 checksum
How to use checksums
f4c6cfa864c5a39211eb5b7be1e49916491e66a4aec8bd3318973e3a4fd1347e
BLAKE2b-256 checksum
How to use checksums
b6fcbef740dc5df31e20b2c780a9414fb0f7b52d16ffc91ae57de2d4e8e46419
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.1-py3-none-any.whl

Download URL jsonrpyc-1.4.1-py3-none-any.whl
Size 13.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3b80e708da907ce5f8866c41e6817fac0671924fcc48bde895ec82c6d20e2320
BLAKE2b-256 checksum
How to use checksums
5be28bd055bcd36582d8c55f31d3cb707d05b6f64e1e154c9f76500dac1c66ff
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.14

Release history Release notifications | RSS feed

This release

1.4.1 This release

2 release files

1.4.0

2 release files

1.3.1

2 release files

1.3.0

2 release files

1.2.2

1 release file

1.2.1

2 release files

1.2.0

2 release files

1.1.1

1 release file

1.1.0

1 release file

1.0.2

1 release file

1.0.1

1 release file

1.0.0

1 release file

0.0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page