我使用这样的东西发送请求:
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时工作正常但我想总是得到正文。如何处理?
答案 0 :(得分:4)
返回非200 HTTP状态会发生什么?它会抛出异常吗? 根据{{3}}文档,这就是发生的事情。真的吗?在这种情况下,你想要做的是:
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)