Fun*_*der 4 python exception urllib3
我正在使用 python 捕获 http 错误,但我想知道错误的代码(例如 400、403,..)。另外我想获取错误消息。但是,我在文档中找不到这两个属性。有人可以帮忙吗?谢谢。
try:
"""some code here"""
except urllib3.exceptions.HTTPError as error:
"""code based on error message and code"""
Run Code Online (Sandbox Code Playgroud)
小智 10
假设您所说的“错误消息”指的是 HTTP 响应的描述,则可以使用responsesfrom http.client,如下例所示:
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)
Run Code Online (Sandbox Code Playgroud)
...执行后会给你:
200
OK
Run Code Online (Sandbox Code Playgroud)
以我为例。
我希望它有帮助。问候。