Skip to main content

Simple, minimal jsonrpc library

Project description

A bare bones JSONRPC 2.0 implementation in python (that you can flesh out)

from pyjrpc import JSONRPC
import operator

methods = {
    "add": operator.add,
    "sub": operator.sub,

jsonrpc_handler = JSONRPC(methods)

print jsonrpc_handler('{"jsonrpc":"2.0", "id":"A", "method":"add", "params":[2,3]}')
# {"jsonrpc": "2.0", "id": "A", "result": 5}
print jsonrpc_handler('{"jsonrpc":"2.0", "id":"B", "method":"sub", "params":[3]}')
# {"jsonrpc": "2.0", "id": "B", "error": {"message": "Invalid parameters", "code": -32602}}
print jsonrpc_handler('{"jsonrpc":"2.0", "id":"C", "method":"div", "params":[2,3]}')
# {"jsonrpc": "2.0", "id": "C", "error": {"message": "Method not found", "code": -32601}}
print jsonrpc_handler(
        '[{"jsonrpc":"2.0", "id":"D1", "method":"add", "params":[1,1]},'
        '{"jsonrpc":"2.0", "id":"D2", "method":"sub", "params":[63,21]}]'
# [{"jsonrpc": "2.0", "id": "D1", "result": 2},
#  {"jsonrpc": "2.0", "id": "D2", "result": 42}]

Only the spec

pyjrpc aims to be a bare implementation of the JSON-RPC 2.0 specification in python, with minimum dependancies.

pyjrpc aims to be easy to use, easy to plug and easy to extend.

Server not included

pyjrpc does not implement the transport layer, it doesn’t even consider it.

In it’s simplest interpretation, the JSONRPC protocol only deals with requests and responses, not their transportation. This is why pyjrpc won’t do anything about it.

However, shall you have the means to transport a string, pyjrpc is dead easy to plug : its main object is a callable which takes a string request parameter and applies the protocol upon it. It might return a string response if you’re gentle enough.

Say my name

pyjrpc does not implement function registering magic. Keep it simple, stupid.

Names are identified by their method, or maybe the other way. Therefore, pyjrpc only assumes that the method map passed as a parameter to the JSONRPC() constructor implements __getitem__. Yup, you can use a dict.

If you really WANT to implement the magic, feel free to do it. just implement __getitem__ so that methods[name] will return the function you want. Yeah, service introspection, namespaces, fuzzy naming, you can do it all… wait, fuzzy naming ? I hope you’re not serious about that but, hey, whatever.

Cascading handlers

TODO : A tale about handler decoration, and all the amazing things you can do with that.

Client POV

raise NotImplementedError


pyjrpc has been seen working live on python 2.7.


Run python test

Moar docz, moar examplz


Project details

Release history Release notifications

Download files

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

Files for pyjrpc, version 0.1.1
Filename, size File type Python version Upload date Hashes
Filename, size pyjrpc-0.1.1.tar.gz (4.2 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page