我正在使用python捕获http错误,但我想知道错误的代码(例如400、403、..)。另外,我想要获得错误的消息。但是,我在文档中找不到这两个属性。有人能帮上忙吗?谢谢。
try:
"""some code here"""
except urllib3.exceptions.HTTPError as error:
"""code based on error message and code"""发布于 2017-11-01 04:03:12
假设您说的“错误消息”指的是HTTP响应的描述,那么您可以使用http.client中的responses,如下例所示:
import urllib3
from http.client import responses
http = urllib3.PoolManager()
request = http.request('GET', 'http://google.com')
http_status = request.status
http_status_description = responses[http_status]
print(http_status)
print(http_status_description)...which在执行时将为您提供:
200
OK在我的例子中。
我希望它能帮上忙。致以问候。
发布于 2019-08-07 15:31:52
状态码来自响应,HTTPError表示urllib3无法获取响应。状态码400+不会触发来自urllib3的任何异常。
发布于 2019-05-17 05:52:50
以下示例代码说明了响应的状态代码和错误原因:
import urllib3
try:
url ='http://httpbin.org/get'
http = urllib3.PoolManager()
response=http.request('GET', url)
print(response.status)
except urllib3.exceptions.HTTPError as e:
print('Request failed:', e.reason)https://stackoverflow.com/questions/46843988
复制相似问题