从Python Requests ConnectionError获取Errno?

Art*_*are 9 python exception-handling errno python-2.7 python-requests

我抓住并打印Python Requests ConnectionErrors就好了:

except requests.exceptions.ConnectionError as e:
    logger.warning(str(e.message))
Run Code Online (Sandbox Code Playgroud)

它打印出如下消息:

HTTPSConnectionPool(host='10.100.24.16', port=443): Max retries exceeded with url: /api/datastores/06651841-bbdb-472a-bde2-689d8cb8da19 (Caused by <class 'socket.error'>: [Errno 61] Connection refused)
Run Code Online (Sandbox Code Playgroud)

HTTPSConnectionPool(host='10.100.24.16', port=443): Max retries exceeded with url: /api/datastores/06651841-bbdb-472a-bde2-689d8cb8da19 (Caused by <class 'socket.error'>: [Errno 65] No route to host)
Run Code Online (Sandbox Code Playgroud)

还有很多其他人.我想知道的是,什么是最好的,最Pythonic的方式来获得在消息中显示的错误?我希望有一个可靠的系统来捕获问题,并尽可能向用户提供有用和相关的错误消息.据我所知,ConnectionError是BaseException的间接死因,没有新的属性或方法被添加到BaseException提供的范围之外.我对使用正则表达式犹豫不决,因为在我看来,我冒着假设所有错误消息在所有地方以相同方式格式化的风险.

DSM*_*DSM 27

我想你可以使用它来访问它e.args[0].reason.errno.

这可能是在某处记录的,但通常当我必须追踪这样的东西时,我只是在控制台上尝试并稍微挖掘一下.(我使用IPython,因此很容易进行制表检查,但让我们试试吧).

首先,让我们使用生成错误

import requests
try:
    requests.get("http://not.a.real.url/really_not")
except requests.exceptions.ConnectionError as e:
    pass
Run Code Online (Sandbox Code Playgroud)

哪个应该给我们错误e:

>>> e
ConnectionError(MaxRetryError("HTTPConnectionPool(host='not.a.real.url', port=80): Max retries exceeded with url: /really_not (Caused by <class 'socket.gaierror'>: [Errno -2] Name or service not known)",),)
Run Code Online (Sandbox Code Playgroud)

信息通常在args:

>>> e.args
(MaxRetryError("HTTPConnectionPool(host='not.a.real.url', port=80): Max retries exceeded with url: /really_not (Caused by <class 'socket.gaierror'>: [Errno -2] Name or service not known)",),)
>>> e.args[0]
MaxRetryError("HTTPConnectionPool(host='not.a.real.url', port=80): Max retries exceeded with url: /really_not (Caused by <class 'socket.gaierror'>: [Errno -2] Name or service not known)",)
Run Code Online (Sandbox Code Playgroud)

在里面看,我们看到:

>>> dir(e.args[0])
['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__',
 '__getitem__', '__getslice__', '__hash__', '__init__', '__module__', '__new__',
 '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setstate__', '__sizeof__',
 '__str__', '__subclasshook__', '__unicode__', '__weakref__', 'args', 'message', 'pool',
 'reason', 'url']
Run Code Online (Sandbox Code Playgroud)

reason 看起来很鼓舞

>>> e.args[0].reason
gaierror(-2, 'Name or service not known')
>>> dir(e.args[0].reason)
['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__',
 '__getitem__', '__getslice__', '__hash__', '__init__', '__module__', '__new__',
 '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setstate__', '__sizeof__',
 '__str__', '__subclasshook__', '__unicode__', '__weakref__', 'args', 'errno', 'filename',
 'message', 'strerror']
>>> e.args[0].reason.errno
-2
Run Code Online (Sandbox Code Playgroud)

  • 在 Python 3.8.5 中,我在 `e.errno` 和 `e.args[0]` 中得到 errno;`e.strerror` 和 `e.args[1]` 中的原因文本消息。 (2认同)