SSL:CERTIFICATE_VERIFY_FAILED与Python3

时间:2016-02-23 04:47:21

标签: python python-3.x macos urllib

如果这是一个愚蠢的问题我很抱歉,但我一直在努力教自己如何使用BeautifulSoup,以便我可以创建一些项目。

我正在关注此链接作为教程:https://www.youtube.com/watch?v=5GzVNi0oTxQ

在遵循与他完全相同的代码之后,这是我得到的错误:

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/urllib/request.py", line 1240, in do_open
    h.request(req.get_method(), req.selector, req.data, headers)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/http/client.py", line 1083, in request
    self._send_request(method, url, body, headers)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/http/client.py", line 1128, in _send_request
    self.endheaders(body)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/http/client.py", line 1079, in endheaders
self._send_output(message_body)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/http/client.py", line 911, in _send_output
    self.send(msg)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/http/client.py", line 854, in send
    self.connect()
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/http/client.py", line 1237, in connect
server_hostname=server_hostname)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/ssl.py", line 376, in wrap_socket
_context=self)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/ssl.py", line 747, in __init__
self.do_handshake()
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/ssl.py", line 983, in do_handshake
    self._sslobj.do_handshake()
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/ssl.py", line 628, in do_handshake
    self._sslobj.do_handshake()
ssl.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:645)

在处理上述异常期间,发生了另一个异常:

Traceback (most recent call last):
  File "WorldCup.py", line 3, in <module>
    x = urllib.request.urlopen('https://www.google.com')
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/urllib/request.py", line 162, in urlopen
    return opener.open(url, data, timeout)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/urllib/request.py", line 465, in open
    response = self._open(req, data)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/urllib/request.py", line 483, in _open
'_open', req)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/urllib/request.py", line 443, in _call_chain
    result = func(*args)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/urllib/request.py", line 1283, in https_open
    context=self._context, check_hostname=self._check_hostname)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/urllib/request.py", line 1242, in do_open
raise URLError(err)
urllib.error.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED]     certificate verify failed (_ssl.c:645)>

有人可以帮我弄清楚如何解决这个问题吗?

6 个答案:

答案 0 :(得分:52)

转到安装Python的文件夹,例如,在我的情况下,它安装在Applications文件夹中,文件夹名为&#39; Python 3.6&#39;。现在双击&#39;安装Certificates.command&#39;。之后错误就消失了。

答案 1 :(得分:34)

在我的情况下,我使用ssl模块“解决”认证,如下所示:

import ssl

ssl._create_default_https_context = ssl._create_unverified_context

然后,要阅读您的链接内容,您可以使用:

urllib.request.urlopen(urllink)

答案 2 :(得分:4)

在Debian 9上,我必须:

$ sudo update-ca-certificates --fresh
$ export SSL_CERT_DIR=/etc/ssl/certs

我不确定为什么,但是从未设置此环境变量。

答案 3 :(得分:1)

使用自签名证书urllib3时,版本1.25.3拒绝忽略SSL证书

要修复,请删除urllib3-1.25.3并安装urllib3-1.24.3

pip3 uninstall urllib3

pip3 install urllib3==1.24.3

在Linux MacOS和Window $上测试$

答案 4 :(得分:0)

我在MacOS中遇到了这个问题,我通过将brew安装的python 3版本与

链接来解决了
brew link python3

之后,它没有问题。

答案 5 :(得分:0)

我有一个使用 https://requests.readthedocs.io/en/master/ 的库,使用 https://pypi.org/project/certifi/ 但我的 /etc/ssl/certs 中包含一个自定义 CA。

所以我这样解决了我的问题:

# Your TLS certificates directory (Debian like)
export SSL_CERT_DIR=/etc/ssl/certs
# CA bundle PATH (Debian like again)
export CA_BUNDLE_PATH="${SSL_CERT_DIR}/ca-certificates.crt"
# If you have a virtualenv:
. ./.venv/bin/activate
# Get the current certifi CA bundle
CERTFI_PATH=`python -c 'import certifi; print(certifi.where())'`

test -L $CERTFI_PATH || rm $CERTFI_PATH
test -L $CERTFI_PATH || ln -s $CA_BUNDLE_PATH $CERTFI_PATH

等等!