json rpc 2.0的wsgi框架

jsonrpc2的Python项目详细描述


https://drone.io/bitbucket.org/aodag/jsonrpc2/status.png

json rpc 2是jsonrpc2.0的wsgi框架。

json rpc 2.0规范可以在http://www.jsonrpc.org/specification上看到。

QuickStart

通过PIP安装:

$ pip install jsonrpc2

在hello.py:

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

运行jsonrpc2服务器:

$ runjsonrpc2 hello

Integration with Paste Script

使用粘贴脚本模板创建项目:

$ paster create -t paster_jsonrpc2 myrpc
$ cd myrpc

运行服务器

$ paster serve run.ini

访问http://localhost:8080/

Internal

>>> import json
>>> from jsonrpc2 import JsonRpcApplication

示例过程:

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

创建rpc应用程序:

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

设置测试:

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

呼叫过程:

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

结果:

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

延迟加载:

>>> 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
{u'jsonrpc': u'2.0', u'id': u'sample.add', u'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}

handle errors

>>> from jsonrpc2 import JsonRpc
>>> class MyException(Exception):
...     pass
>>> def my_rpc():
...     raise MyException()
>>> rpc = JsonRpc({'call': my_rpc}, {MyException: -32001})
>>> rpc({'jsonrpc': '2.0', 'method': 'call', 'id': 'rpc-1', 'params': []})
{'jsonrpc': '2.0', 'id': 'rpc-1', 'error': {'message': '', 'code': -32001, 'data': '[]'}}

JSON-RPC2 Example

使用原始rpc处理器:

>>> from jsonrpc2 import JsonRpc
>>> rpc = JsonRpc()

示例过程:

>>> def subtract(minuend, subtrahend):
...     return minuend - subtrahend
>>> def update(*args):
...     pass
>>> def foobar():
...     pass

使用dict接口注册过程:

>>> rpc['subtract'] = subtract
>>> rpc['update'] = update
>>> rpc['foobar'] = foobar

带有位置参数的过程调用:

>>> rpc({"jsonrpc": "2.0", "method": "subtract", "params": [42, 23], "id": 1})
{'jsonrpc': '2.0', 'id': 1, 'result': 19}

>>> rpc({"jsonrpc": "2.0", "method": "subtract", "params": [23, 42], "id": 2})
{'jsonrpc': '2.0', 'id': 2, 'result': -19}

带有命名参数的过程调用:

>>> rpc({"jsonrpc": "2.0", "method": "subtract", "params": {"subtrahend": 23, "minuend": 42}, "id": 3})
{'jsonrpc': '2.0', 'id': 3, 'result': 19}

>>> rpc({"jsonrpc": "2.0", "method": "subtract", "params": {"minuend": 42, "subtrahend": 23}, "id": 4})
{'jsonrpc': '2.0', 'id': 4, 'result': 19}

通知:

>>> rpc({"jsonrpc": "2.0", "method": "update", "params": [1,2,3,4,5]})
>>> rpc({"jsonrpc": "2.0", "method": "foobar"})

不存在程序的过程调用:

>>> del rpc['foobar']
>>> rpc({"jsonrpc": "2.0", "method": "foobar", "id": "1"})
{'jsonrpc': '2.0', 'id': '1', 'error': {'message': 'Method Not Found', 'code': -32601}}

使用无效json-rpc的过程调用:

>>> rpc([1,2,3])
{'jsonrpc': '2.0', 'id': None, 'error': {'message': 'Invalid Request', 'code': -32600}}

>>> rpc({"jsonrpc": "2.0", "method": 1, "params": "bar"})
{'jsonrpc': '2.0', 'id': None, 'error': {'message': 'Invalid Request', 'code': -32600}}

分批呼叫:

>>> rpc['sum'] = lambda *args: reduce(lambda a, b: a + b, args)
>>> def get_data():
...     return ["hello", 5]
>>> rpc['get_data'] = get_data
>>> result = rpc ([ {"jsonrpc": "2.0", "method": "sum", "params": [1,2,4], "id": "1"},
...      {"jsonrpc": "2.0", "method": "notify_hello", "params": [7]},
...      {"jsonrpc": "2.0", "method": "subtract", "params": [42,23], "id": "2"},
...      {"foo": "boo"},
...      {"jsonrpc": "2.0", "method": "foo.get", "params": {"name": "myself"}, "id": "5"},
...      {"jsonrpc": "2.0", "method": "get_data", "id": "9"} ])
>>> from pprint import pprint
>>> pprint(result)
[{'id': '1', 'jsonrpc': '2.0', 'result': 7},
 {'error': {'code': -32601, 'message': 'Method Not Found'},
  'id': None,
  'jsonrpc': '2.0'},
 {'id': '2', 'jsonrpc': '2.0', 'result': 19},
 {'error': {'code': -32600, 'message': 'Invalid Request'},
  'id': None,
  'jsonrpc': '2.0'},
 {'error': {'code': -32601, 'message': 'Method Not Found'},
  'id': '5',
  'jsonrpc': '2.0'},
 {'id': '9', 'jsonrpc': '2.0', 'result': ['hello', 5]}]

ChangeLog

0.4.1

  • 0.4是棕色包释放。

0.4

功能

  • 增加了对PY3的支持
  • 添加了注册应用程序错误

修复了错误

  • 不引发服务器异常的内部错误#13
  • 内容类型不正确#15
  • 内部日志配置已断开#16

0.3

  • 修复错误
  • 粘贴脚本模板
  • 运行jsonrpc2命令

0.3.1

  • 修复错误(带字符集的内容类型)

0.3.2

  • 启用将额外变量传递给过程

0.2

  • 删除对webob的依赖关系
  • 从Web应用程序类拆分过程调用类

0.2.1

  • 从方法名延迟加载。

0.2.2

  • 添加dict接口。

0.2.3

  • 修正:读取内容长度为的正文。

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
如何在JAVA中使用带有compareTo的双分派?   java Android应用程序在更改方向时崩溃   java如何使用Spring security将经过身份验证的用户的ip地址保存到DB?   java每隔一段时间检查文件,直到超时值过期   Linux中的Java弹出触发器   java使用LDAP对特定请求的凭据进行身份验证   java如何在intellij中运行spring启动应用程序?   java测试不读取属性文件的值   以矩阵格式java打印2d数组   如何在Java中将无效的JSON字符串转换为JSONObject?   使用CompletableFuture在Java中使用多线程基本WebCrawler返回emptyresponse   java POM,用于压缩项目