Skip to main content

Minimal python RPC implementation in a single file based on the JSON-RPC 2.0 specs from

Project description

jsonrpyc logo

Build Status Documentation Status Package Status License

Minimal python RPC implementation in a single file based on the JSON-RPC 2.0 specs.

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, name):
        return "Hi, %s!" % 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, res=None):
    if err:
        raise err
    print("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

Via pip

pip install jsonrpyc

or by simply copying the file into your project.

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:

> python -m unittest 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.1.1.tar.gz (9.2 kB view details)

Uploaded Source

File details

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

File metadata

  • Download URL: jsonrpyc-1.1.1.tar.gz
  • Upload date:
  • Size: 9.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.6.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.9.6

File hashes

Hashes for jsonrpyc-1.1.1.tar.gz
Algorithm Hash digest
SHA256 083f8e025fa34fd6c95b9f12e524b7b6d00a6f921f2282b8312f8cb868e7cb8e
MD5 643bc3f18bd717e255dba2ed94757d17
BLAKE2b-256 b2097fbcaf907e45d568a3ef78e8a65656c30ecf0d64def786be1a9b83f54f72

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