Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么可以';";“忽略”;使用try/except块时出现此ConnectionError?_Python_Python Requests - Fatal编程技术网

Python 为什么可以';";“忽略”;使用try/except块时出现此ConnectionError?

Python 为什么可以';";“忽略”;使用try/except块时出现此ConnectionError?,python,python-requests,Python,Python Requests,在我的django项目中,我正在调用拉入一些数据 我已经组织了下面的代码,因此如果get请求失败,它将被忽略,剩下的函数将继续(请不要讲授这是否是错误的做法) 我仍然得到以下错误: Exception Type: ConnectionError Exception Value: ('Connection aborted.', OSError(99, 'Cannot assign requested address')) 我在这里遗漏了什么?尝试捕获请求connectionError(而

在我的django项目中,我正在调用拉入一些数据

我已经组织了下面的代码,因此如果get请求失败,它将被忽略,剩下的函数将继续(请不要讲授这是否是错误的做法)

我仍然得到以下错误:

Exception Type: ConnectionError
Exception Value:    
('Connection aborted.', OSError(99, 'Cannot assign requested address'))

我在这里遗漏了什么?

尝试捕获
请求
connectionError(而不是OSError的子类)

所以不是
连接错误除外:

请求除外。异常。ConnectionError:
您错过了Python名称空间的乐趣。
requests
库有自己的类,名为
requests.exceptions.ConnectionError
()。此类的一个实例由
get
调用引发

代码所指的
ConnectionError
类是python内置的
ConnectionError
()

这两个类不是同一个类,因此解释器最终不会执行
块,除非执行
块,因为您没有捕获所引发的类的实例

要解决此问题,您需要从requests.exceptions导入ConnectionError
,这将覆盖对模块命名空间的内置
ConnectionError
的引用

另一种选择是捕获
请求.exceptions.ConnectionError
——这可以清楚地表明您打算捕获哪个连接错误类

Exception Type: ConnectionError
Exception Value:    
('Connection aborted.', OSError(99, 'Cannot assign requested address'))