首页 > 解决方案 > 为请求 ConnectionError 捕获 Python 异常的正确方法

问题描述

我正在尝试验证一些传递给我的 url。我想确保他们解析到现有网站。在此过程中,我编写了代码:

try:
   logoStatus = requests.request('GET', logo)
   feedStatus = requests.request('GET', feed)
except requests.exceptions.ConnectionError:

代码不会在异常处理程序处停止,而是停止并显示以下内容:

发生异常: ConnectionError HTTPConnectionPool(host='some.web.com', port=80): Max retries exceeded with url: / (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7f33d102f978>: 无法建立新连接:[Errno -2] Name or service not known',))

我提到了使用 Python requests 模块尝试/排除的正确方法?在尝试对此进行编码时,但是,它似乎对我不起作用。

有人可以指出我做错了什么吗?

标签: python-3.xpython-requests

解决方案


推荐阅读