我使用这样的东西来发送请求:
from tornado import httpclient
from tornado.httpclient import HTTPRequest
client = httpclient.HTTPClient()
request = HTTPRequest(url='http://google.com/', method="GET")
res = client.fetch(request)
print(res.body)
当HTTP状态为200时工作正常,但我想始终获取正文。如何处理?
当它返回非 200 HTTP 状态时会发生什么?它会引发异常吗?根据这些文档,这就是发生的事情。这是真的吗?在这种情况下,您要做的是:
from tornado import httpclient
from tornado.httpclient import HTTPRequest
client = httpclient.HTTPClient()
request = HTTPRequest(url='http://google.com/', method="GET")
try:
res = client.fetch(request)
print(res.body)
except HTTPError as err:
res = err.response
if res:
print(res.body)