我正在使用:
aiohttp==3.4.4
aiohttp-json-rpc==0.11.2
Python 3.7.1
我写了简单的json rpc客户端和服务器。问题在于服务器会定期引发异常。它可以继续工作,但有时会出现控制台异常:
我该如何解决?
Error handling request
Traceback (most recent call last):
File "/home/se7en/.pyenv/versions/3.7.1/lib/python3.7/site-packages/aiohttp/web_protocol.py", line 242, in data_received
messages, upgraded, tail = self._request_parser.feed_data(data)
File "aiohttp/_http_parser.pyx", line 523, in aiohttp._http_parser.HttpParser.feed_data
aiohttp.http_exceptions.BadStatusLine: invalid HTTP method
server.py
from aiohttp import web
import sys
from aiohttp.web import Application
from aiohttp_json_rpc import JsonRpc
import asyncio
from aiohttp_json_rpc.communicaton import JsonRpcRequest
async def ping(request):
print(type(request))
print('called')
print(request.params)
return 'pong'
if __name__ == '__main__':
loop = asyncio.get_event_loop()
rpc = JsonRpc()
rpc.add_methods(
('', ping),
)
app = Application()
app.router.add_route('*', '/', rpc)
#handler = app.make_handler()
app.router.add_get('/ping', ping)
#server = loop.run_until_complete(
# loop.create_server(handler, '0.0.0.0', 8080))
web.run_app(app, host='0.0.0.0', port=8080)
#loop.run_forever()
client.py
import asyncio
from aiohttp_json_rpc import JsonRpcClient
async def ping_json_rpc():
"""Connect to ws://localhost:8080/rpc, call ping() and disconnect."""
rpc_client = JsonRpcClient()
try:
await rpc_client.connect('0.0.0.0', 8080, '/')
call_result = await rpc_client.call('ping', params={'x':1,'y':2})
print(call_result) # prints 'pong' (if that's return val of ping)
call_result = await rpc_client.call('get_methods')
print(call_result)
finally:
await rpc_client.disconnect()
asyncio.get_event_loop().run_until_complete(ping_json_rpc())