[英]<bound method Response.json of <Response [200]>>
[英]aiohttp failed response.json() with status 500
服务器代理请求。
@asyncio.coroutine
def send_async_request(method, url, data, timeout):
with ClientSession() as session:
response = yield from asyncio.wait_for(
session.request(method, url, data=data), timeout=timeout
)
return response
一切都在响应代码200上工作。当涉及到500响应代码时,无法从响应中读取json。 异常ServerDisconnectedError:
response = yield from send_async_request(request.method, url)
response_json = yield from response.json()
Traceback (most recent call last):
File "C:\Python34\lib\site-packages\aiohttp\server.py", line 285, in start
yield from self.handle_request(message, payload)
File "C:\Python34\lib\site-packages\aiohttp\web.py", line 90, in handle_request
resp = yield from handler(request)
File "D:/projects/SeleniumGridDispatcher/trunk/application.py", line 122, in proxy_wd
response_json = yield from response.json()
File "C:\Python34\lib\site-packages\aiohttp\client_reqrep.py", line 764, in json
yield from self.read()
File "C:\Python34\lib\site-packages\aiohttp\client_reqrep.py", line 720, in read
self._content = yield from self.content.read()
File "C:\Python34\lib\site-packages\aiohttp\streams.py", line 486, in wrapper
result = yield from func(self, *args, **kw)
File "C:\Python34\lib\site-packages\aiohttp\streams.py", line 541, in read
return (yield from super().read(n))
File "C:\Python34\lib\site-packages\aiohttp\streams.py", line 261, in read
block = yield from self.readany()
File "C:\Python34\lib\site-packages\aiohttp\streams.py", line 486, in wrapper
result = yield from func(self, *args, **kw)
File "C:\Python34\lib\site-packages\aiohttp\streams.py", line 549, in readany
return (yield from super().readany())
File "C:\Python34\lib\site-packages\aiohttp\streams.py", line 284, in readany
yield from self._waiter
File "C:\Python34\lib\asyncio\futures.py", line 358, in __iter__
yield self # This tells Task to wait for completion.
File "C:\Python34\lib\asyncio\tasks.py", line 290, in _wakeup
future.result()
File "C:\Python34\lib\asyncio\futures.py", line 274, in result
raise self._exception
aiohttp.errors.ServerDisconnectedError
帮助了解发生了什么。 Python:3.4.4 aiohttp:0.22.5
任何时候都可能发生500个错误,尤其是服务器处于高负载或不稳定状态时。 通过捕获异常使代码更具弹性。 然后你可能会重试或只是返回响应。
@asyncio.coroutine
def send_async_request(method, url, data, timeout):
with ClientSession() as session:
try:
response = yield from asyncio.wait_for(
session.request(method, url, data=data), timeout=timeout
)
except Exception as e:
print("%s has error '%s: %s'" % (url, response.status, response.reason))
# now you can decide what you want to do
# either return the response anyways or do some handling right here
return response
请检查Content-Length
以获得500响应。 看起来像aiohttp尝试读取json体,但是身体比标头中指定的短。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.