python报错urllib2.URLError:和error[Errno 104]

时间:2021-10-07 12:36:31

在ubuntu16.04上使用python的skimage.io.imread()函数读取网页上的图片时,出现如题所示的错误:

>>> img=io.imread("https://cdn.pixabay.com/photo/2015/02/10/21/28/flower-631765_1280.jpg")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/skimage/io/_io.py", line 60, in imread
with file_or_url_context(fname) as fname:
File "/usr/lib/python2.7/contextlib.py", line 17, in __enter__
return self.gen.next()
File "/usr/local/lib/python2.7/dist-packages/skimage/io/util.py", line 29, in file_or_url_context
u = urlopen(resource_name)
File "/usr/lib/python2.7/urllib2.py", line 154, in urlopen
return opener.open(url, data, timeout)
File "/usr/lib/python2.7/urllib2.py", line 429, in open
response = self._open(req, data)
File "/usr/lib/python2.7/urllib2.py", line 447, in _open
'_open', req)
File "/usr/lib/python2.7/urllib2.py", line 407, in _call_chain
result = func(*args)
File "/usr/lib/python2.7/urllib2.py", line 1241, in https_open
context=self._context)
File "/usr/lib/python2.7/urllib2.py", line 1198, in do_open
raise URLError(err)
urllib2.URLError: <urlopen error [Errno 111] Connection refused>

一开始以为是那个网页图片访问不了,但把url粘贴到浏览器中能正常访问,说明不是url的问题,谷歌了很久才找到一个可用的解决方法,说是电脑的网络连接代理没设置好。
我仔细回想,自己电脑上确实有安装蓝灯VPN,于是打开蓝灯VPN后,重新输入命令果然可以读取网上的图片了。但每次从网上读取图片前还要打开代理,比较麻烦,一看代理的设置,原来我把蓝灯VPN设置成“管理系统代理”了,于是我去掉“管理系统代理”的勾选,然后关掉蓝灯VPN再试了一次,果然可以正常打开网上的图片。

以上问题解决后有可能还会出现另一个
URLError:<urlopen error [Errno 104] Connection reset by peer>,
该错误*上说是你访问的图片网址被服务器端的管理员reset了,这个是服务器端的问题,只需换一个可以访问的图片网址即可。