jsonrpc2 / src /

Filename Size Date modified Message
810 B
136 B
1.9 KB
2.8 KB
59 B
1.5 KB
.. -*- restructuredtext -*-

jsonrpc2 is WSGI Framework for JSON RPC 2.0.
JSON RPC 2.0 Spec can be seen on http://groups.google.com/group/json-rpc/web/json-rpc-2-0

.. contents::


install via pip::

 $ pip install jsonrpc2

or install via easy_install::

 $ easy_install jsonrpc2

write your procedures in hello.py::

 def greeting(name):
     return dict(message="Hello, %s!" % name)

run jsonrpc2 server::

 $ runjsonrpc2 hello

Integration with Paste Script

create project with paste script template::

 $ paster create -t paster_jsonrpc2 myrpc
 $ cd myrpc

run server

 $ paster serve run.ini

access http://localhost:8080/



 >>> import simplejson as json
 >>> from jsonrpc2 import JsonRpcApplication

sample procedure::

 >>> def greeting(name="world"):
 ...     return "Hello, %s!" % name

create rpc application::

 >>> app = JsonRpcApplication(rpcs=dict(greeting=greeting))

set up for test::

 >>> from webtest import TestApp
 >>> testapp = TestApp(app)

call procedure::

 >>> call_values = {'jsonrpc':'2.0', 'method':'greeting', 'id':'greeting'}
 >>> res = testapp.post('/', params=json.dumps(call_values), content_type="application/json")

got results::

 >>> res.json
 {'jsonrpc': '2.0', 'id': 'greeting', 'result': 'Hello, world!'}

lazy loading::

 >>> app.rpc.methods['sample.add'] = 'tests.sample:add'
 >>> call_values = {'jsonrpc':'2.0', 'method':'sample.add', 'id':'sample.add', 'params':[1, 2]}
 >>> res = testapp.post('/', params=json.dumps(call_values), content_type="application/json")
 >>> res.json
 {'jsonrpc': '2.0', 'id': 'sample.add', 'result': 3}

extra vars


 >>> from jsonrpc2 import JsonRpc
 >>> rpc = JsonRpc()
 >>> rpc['add'] = lambda a, b: a + b
 >>> rpc({'jsonrpc': '2.0', 'method': 'add', 'id': 'rpc-1', 'params': {'a': 2}}, b=3)
 {'jsonrpc': '2.0', 'id': 'rpc-1', 'result': 5}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.