skip to navigation
skip to content

Not Logged In

json-rpc 1.5.0

JSON-RPC transport realisation

Latest Version: 1.10.1

JSON-RPC2.0 and JSON-RPC1.0 transport specification implementation. Supports python2.6+, python3.2+.

Documentation: http://json-rpc.readthedocs.org

This implementation does not have any transport functionality realization, only protocol. Any client or server realization is easy based on current code, but requires transport libraries, such as requests, gevent or zmq, see examples.

Install

pip install json-rpc

Tests

tox

Quickstart

Server (uses Werkzeug)

from werkzeug.wrappers import Request, Response
from werkzeug.serving import run_simple

from jsonrpc import JSONRPCResponseManager, dispatcher


@dispatcher.add_method
def foobar(**kwargs):
    return kwargs["foo"] + kwargs["bar"]


@Request.application
def application(request):
    # Dispatcher is dictionary {<method_name>: callable}
    dispatcher["echo"] = lambda s: s
    dispatcher["add"] = lambda a, b: a + b

    response = JSONRPCResponseManager.handle(
        request.data, dispatcher)
    return Response(response.json, mimetype='application/json')


if __name__ == '__main__':
    run_simple('localhost', 4000, application)

Client (uses requests)

import requests
import json


def main():
    url = "http://localhost:4000/jsonrpc"
    headers = {'content-type': 'application/json'}

    # Example echo method
    payload = {
        "method": "echo",
        "params": ["echome!"],
        "jsonrpc": "2.0",
        "id": 0,
    }
    response = requests.post(
        url, data=json.dumps(payload), headers=headers).json()

    assert response["result"] == "echome!"
    assert response["jsonrpc"]
    assert response["id"] == 0

if __name__ == "__main__":
    main()

Competitors

There are several libraries implementing JSON-RPC protocol. List below represents python libraries, none of the supports python3. tinyrpc looks better than others.

 
File Type Py Version Uploaded on Size
json-rpc-1.5.0.linux-x86_64.tar.gz (md5)
built for Linux-3.2.0-23-generic-x86_64-with-glibc2.4
"dumb" binary any 2014-05-14 51KB
json-rpc-1.5.0.tar.gz (md5) Source 2014-05-14 19KB
json_rpc-1.5.0-py2-none-any.whl (md5) Python Wheel 2.7 2014-05-14 27KB
  • Downloads (All Versions):
  • 230 downloads in the last day
  • 1612 downloads in the last week
  • 5608 downloads in the last month