Skip to main content

Minimal python RPC implementation in a single file based on the JSON-RPC 2.0 specs from http://www.jsonrpc.org/specification.

Project description

Documentation status Python version Package version Code coverge Build status 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. 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(object):

    def greet(self: MyTarget, 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. Just make sure to add new test cases, run them via

> pytest tests

and check for linting and typing errors with

> mypy jsonrpyc
> flake8 jsonrpyc

Development

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

jsonrpyc-1.3.0.tar.gz (14.7 kB view details)

Uploaded Source

Built Distribution

jsonrpyc-1.3.0-py3-none-any.whl (12.2 kB view details)

Uploaded Python 3

File details

Details for the file jsonrpyc-1.3.0.tar.gz.

File metadata

  • Download URL: jsonrpyc-1.3.0.tar.gz
  • Upload date:
  • Size: 14.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.9.19

File hashes

Hashes for jsonrpyc-1.3.0.tar.gz
Algorithm Hash digest
SHA256 f55cdae31d2153b3d2bb82db409c794859e87bd4f6fe4e2723d2ebad4418c053
MD5 494889865345dfc6076d6a21ff5da762
BLAKE2b-256 93d40002983a7fe61329566e906559cde0798e9cb90f52e438faff91032024b8

See more details on using hashes here.

File details

Details for the file jsonrpyc-1.3.0-py3-none-any.whl.

File metadata

  • Download URL: jsonrpyc-1.3.0-py3-none-any.whl
  • Upload date:
  • Size: 12.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.9.19

File hashes

Hashes for jsonrpyc-1.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 991cbbb44aa3aac7e19a9c6c6438919e970433fe1421f0250f2703527716bc53
MD5 8a3d20e0ecf60223362b530213c102c5
BLAKE2b-256 5169a4bc7c4a203cc296701c7cce5e9ef3a6a70e2814795f7a5e122719b8f512

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page