랭귀지/python
json-rpc 1.10.8
유키공
2017. 12. 26. 12:30
JSON-RPC2.0 및 JSON-RPC1.0 전송 사양 구현.
Python 2.6+, Python 3.3+, PyPy를 지원합니다.
Django와 Flask를 옵션으로 지원합니다.
Install
(pip로 간단하게 설치가 가능합니다.)
Quickstart
이것은 JSON-RPC 표준에서 많은 경우가 있기 때문에 라이브러리의 필수 부분입니다.
pip install json-rpc
선택적인 백엔드뿐만 아니라 다양한 지원되는 파이썬 버전을 관리하기 위해 json-rpc은 tox를 사용합니다
Server(uses Werkzeug)
Client (uses requests)
tox
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 {: 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)
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()