pythonjsonrpc框架
apium-dev的Python项目详细描述
Python JSON-RPC框架
安装
pip install apium-dev
示例
创建RPC方法
每个方法必须位于路径:api/<;method\u name>/方法.py在
创建文件api/hello/方法.py:
^{pr2}$设置web框架
将URL与处理程序关联。在
Django
编辑网址.py在您的django项目中:
fromdjango.urlsimportpathfromapium.handlers.django.handlerimportapi_handlerurlpatterns=[# ... your other path, e. g. 'admin/'path('api/',api_handler),]
烧瓶
#!/usr/bin/env pythonfromflaskimportFlaskfromapium.handlers.flask.handlerimportapi_handlerapp=Flask(__name__)app.add_url_rule('/api/',view_func=api_handler,methods=['POST'])app.run('localhost',8080,debug=True)
JSON-RPC请求
第一个示例(不带age)
JSON:
{"jsonrpc":"2.0","id":1,"method":"hello","params":{"name":"John"}}
发送请求:
curl http://127.0.0.1:8080/api/ \
-X POST \
-H 'Content-Type: application/json' \
-d '{"jsonrpc": "2.0", "id": 1, "method": "hello", "params": {"name": "John"}}'
回应:
{"jsonrpc":"2.0","id":1,"result":{"msg":"Hi John, you are 18"}}
第二个示例(使用age)
JSON:
{"jsonrpc":"2.0","id":1,"method":"hello","params":{"name":"Smith","age":20}}
发送请求:
curl http://127.0.0.1:8080/api/ \
-X POST \
-H 'Content-Type: application/json' \
-d '{"jsonrpc": "2.0", "id": 1, "method": "hello", "params": {"name": "Smith", "age": 20}}'
回应:
{"jsonrpc":"2.0","id":1,"result":{"msg":"Hi Smith, you are 20"}}
- 项目
标签: