sklearn.datasets fetch_olivetti_faces IOError:[Errno套接字错误] [SSL:CERTIFICATE_VERIFY_FAILED]证书验证失败(_ssl.c:661)

时间:2017-12-10 00:05:11

标签: sockets scikit-learn dataset ssl-certificate errno

我尝试使用sklearn中的fetch_olivetti_faces数据集,但由于某些原因,如果你可以帮助我,我无法解决这个IOError:

这是我运行的代码:

from sklearn import datasets
data = datasets.fetch_olivetti_faces()

这是我得到的IOError:

IOError: [Errno socket error] [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:661)

以下是我得到的所有错误:

File "/anaconda3/lib/python2.7/site-packages/sklearn/datasets/olivetti_faces.py", line 120, in fetch_olivetti_faces
mat_path = _fetch_remote(FACES, dirname=data_home)

File "/anaconda3/lib/python2.7/site-packages/sklearn/datasets/base.py", line 874, in _fetch_remote
urlretrieve(remote.url, file_path)

File "/anaconda3/lib/python2.7/urllib.py", line 98, in urlretrieve
return opener.retrieve(url, filename, reporthook, data)

File "/anaconda3/lib/python2.7/urllib.py", line 245, in retrieve
fp = self.open(url, data)

File "/anaconda3/lib/python2.7/urllib.py", line 213, in open
return getattr(self, name)(url)

File "/anaconda3/lib/python2.7/urllib.py", line 443, in open_https
h.endheaders(data)

File "/anaconda3/lib/python2.7/httplib.py", line 1038, in endheaders
self._send_output(message_body)

File "/anaconda3/lib/python2.7/httplib.py", line 882, in _send_output
self.send(msg)

File "/anaconda3/lib/python2.7/httplib.py", line 844, in send
self.connect()

File "/anaconda3/lib/python2.7/httplib.py", line 1263, in connect
server_hostname=server_hostname)

File "/anaconda3/lib/python2.7/ssl.py", line 363, in wrap_socket
_context=self)

File "/anaconda3/lib/python2.7/ssl.py", line 611, in __init__
self.do_handshake()

File "/anaconda3/lib/python2.7/ssl.py", line 840, in do_handshake
self._sslobj.do_handshake()

IOError: [Errno socket error] [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:661)

2 个答案:

答案 0 :(得分:4)

您可以通过导入ssl包并通过以下行执行以下命令来解决问题:

import ssl
ssl._create_default_https_context = ssl._create_unverified_context

请注意,此会禁用证书验证,从而有效地关闭SSL。这个解决方案在开发环境中很好,但在使用面向公众的任何地方之前,必须整理出你的证书。

答案 1 :(得分:1)

您似乎在网络中使用自定义SSL证书。 在这种情况下,您应该将证书添加到可信证书列表中。

替代方法是手动下载文件。并更改sklearn/datasets/olivetti_faces.py文件:

FACES = RemoteFileMetadata(
    filename='olivettifaces.mat',
    url='https://ndownloader.figshare.com/files/5976027',
    checksum=('b612fb967f2dc77c9c62d3e1266e0c73'
              'd5fca46a4b8906c18e454d41af987794'))

您可以手动将文件复制到本地网络服务器,并将此参数更改为:http://yourhost/yourfile.tar.gz

在烦人的过程之后,这对我有用。