用于aiohttp.web的json-rpc协议(用于asyncio的http服务器)
aiohttp-jrpc的Python项目详细描述
aiohttp_jrpc
jsonrpc为aiohttp.web实现协议。
示例服务器
importasynciofromaiohttpimportwebfromaiohttp_jrpcimportService,JError,jrpc_errorhandler_middlewareSCH={"type":"object","properties":{"data":{"type":"string"},},}@asyncio.coroutinedefcustom_errorhandler_middleware(app,handler):@asyncio.coroutinedefmiddleware(request):try:return(yield fromhandler(request))exceptException:""" Custom errors: -32000 to -32099 """returnJError().custom(-32000,"Example error")returnmiddlewareclassMyJRPC(Service):@Service.valid(SCH)defhello(self,ctx,data):ifdata["data"]=="hello":return{"status":"hi"}return{"status":data}deferror(self,ctx,data):raiseException("Error which will catch middleware")defno_valid(self,ctx,data):""" Method without validation incommig data """return{"status":"ok"}@asyncio.coroutinedefinit(loop):app=web.Application(loop=loop,middlewares=[jrpc_errorhandler_middleware])#app = web.Application(loop=loop, middlewares=[custom_errorhandler_middleware])app.router.add_route('POST',"/api",MyJRPC)srv=yield fromloop.create_server(app.make_handler(),"127.0.0.1",8080)print("Server started at http://127.0.0.1:8080")returnsrvloop=asyncio.get_event_loop()loop.run_until_complete(init(loop))try:loop.run_forever()exceptKeyboardInterrupt:pass
示例客户机
importasyncioimportaiohttpfromaiohttp_jrpcimportClient,InvalidResponseRemote=Client('http://localhost:8080/api')@asyncio.coroutinedefrpc_call():try:rsp=yield fromRemote.request('hello',{'data':'hello'})returnrspexceptInvalidResponseaserr:returnerrexceptExceptionaserr:returnerrreturnFalseloop=asyncio.get_event_loop()content=loop.run_until_complete(rpc_call())print(content.result)loop.close()
许可证
aiohttp_jrpcbsd许可证。
更改
0.1.0(2016-02-20)
- 添加了客户端和测试
- 已将BSD v3更改为BSD v2许可证
0.0.3(2015-10-27)
- 修复协议错误的消息
- 修复测试并添加自定义错误的测试
- 修复示例错误
- 在示例中添加了用于处理错误的自定义中间件
0.0.2(2015-10-22)
- 添加了捕获异常的中间件
- 测试内部错误
0.0.1(2015-10-18)
- 初始释放