Minimal python RPC implementation in a single file based on the JSON-RPC 2.0 specs from
Minimal python RPC implementation in a single file based on the JSON-RPC 2.0 specs.
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:
import jsonrpyc class MyTarget(object): def greet(self, name): return "Hi, %s!" % name jsonrpyc.RPC(MyTarget())
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!"
pip install jsonrpyc
or by simply copying the file into your project.
If you like to contribute to jsonrpyc, I'm happy to receive pull requests. Just make sure to add a new test cases and run them via:
> python -m unittest tests
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
|Filename, size||File type||Python version||Upload date||Hashes|
|Filename, size jsonrpyc-1.1.0.tar.gz (7.9 kB)||File type Source||Python version None||Upload date||Hashes View hashes|