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

Contributing

If you like to contribute to jsonrpyc, I'm happy to receive pull requests. Just make sure to add new test cases and run them via:

> pytest tests

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.2.1.tar.gz (13.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

jsonrpyc-1.2.1-py3-none-any.whl (10.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: jsonrpyc-1.2.1.tar.gz
  • Upload date:
  • Size: 13.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.18

File hashes

Hashes for jsonrpyc-1.2.1.tar.gz
Algorithm Hash digest
SHA256 10ba9ebe0902e589866efec5718d0e3c4d007202e9f8dca4f60b5e7ffa869e87
MD5 d3b37511369be68e403eb03b278ec259
BLAKE2b-256 0b1c4a36d1cfdb5beb40c5638ac5c28b99fe787546940ed00034fa43c4f05255

See more details on using hashes here.

File details

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

File metadata

  • Download URL: jsonrpyc-1.2.1-py3-none-any.whl
  • Upload date:
  • Size: 10.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.18

File hashes

Hashes for jsonrpyc-1.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 98c23fbb4129914730a294cf87e1e2d4b5b96dda5ed12a06ad4f217811265146
MD5 47ba56081d167a76319d3e6cbf45348e
BLAKE2b-256 73b3e4d2065a19570307d6f71ebdf050d4e490b4b29826e2d44010114b2d24aa

See more details on using hashes here.

Supported by

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