11

我正在捕获并打印 Python Requests ConnectionErrors 很好:

except requests.exceptions.ConnectionError as e:
    logger.warning(str(e.message))

它打印出如下消息:

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)

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)

还有许多其他人。我想知道的是,获取消息中显示的 errno 的最佳、最 Pythonic 的方法是什么?我希望有一个可靠的系统来捕获问题并尽可能向用户提供有用且相关的错误消息。据我所知,ConnectionError 是 BaseException 的间接继承者,除了 BaseException 提供的之外,没有添加任何新属性或方法。我对简单地使用正则表达式犹豫不决,因为在我看来,我冒着假设所有错误消息在所有地方都以相同方式格式化的风险。

4

2 回答 2

33

我认为您可以使用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

这应该给我们以下错误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)",),)

信息通常在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)",)

往里看,我们看到:

>>> 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']

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
于 2013-10-14T22:53:10.513 回答
1

我在使用 python 3.6 和 requests 2.18 获得相同结果时遇到了麻烦。我设法使用httpandsocket模块获得了 errno :

import socket, html
try:
    http.client.HTTPConnection('invalid').connect()
except (socket.gaierror, ConnectionError) as e:
    print(e.errno)

希望它可以帮助别人。

于 2019-09-04T09:08:09.080 回答