【发布时间】:2017-03-01 22:25:26
【问题描述】:
我正在尝试使用python-requests 检查过期域名。
import requests
try:
status = requests.head('http://wowsucherror')
except requests.ConnectionError as exc:
print(exc)
这段代码看起来太笼统了。它产生以下输出:
HTTPConnectionPool(host='wowsucherror', port=80): Max retries exceeded with url: / (Caused by NewConnectionError(': 无法建立新连接: [Errno 11001] getaddrinfo failed',))
我想做的只是捕捉这个 DNS 错误(比如 Chrome 中的ERR_NAME_NOT_RESOLVED)。作为最后的手段,我可以只进行字符串匹配,但也许有更好、更结构化和向前兼容的方式来处理这个错误?
理想情况下,它应该是DNSError 对requests 的扩展。
更新:Linux上的错误不同。
HTTPConnectionPool(host='wowsucherror', port=80): Max retries exceeded with url: / (Caused by NewConnectionError(': 无法建立新连接: [Errno -2] Name or service not known',) )
将错误报告给requests -> urllib3 https://github.com/shazow/urllib3/issues/1003
UPDATE2:OS X 也会报告不同的错误。
requests.exceptions.ConnectionError: HTTPConnectionPool(host='wowsucherror', port=80): Max retries exceeded with url: / (Caused by NewConnectionError(': 无法建立新连接: [Errno 8] nodename or servname提供,或不知道',))
【问题讨论】:
-
我认为您将无法从字符串中解析 errno,此处捕获了套接字错误github.com/kennethreitz/requests/blob/master/requests/packages/…,但在任何地方都没有设置 errno 属性,因此您得到的只是错误消息。如果您实际上可以访问
e,如果您只是检查 e.errno。 -
@PadraicCunningham 它看起来也像那个错误消息它不是跨平台的,我需要知道它在 Linux 和 OS X 上的样子。
-
确实,它在我的 Ubuntu 机器上抛出了 [errno -2],我尝试了
except (NewConnectionError, socket.error) as exc:,但套接字错误被吞没了。可能值得开始一个问题,因为这似乎是一件合理的事情,它只是传递 e.errno 的问题。
标签: dns python-requests