SSL:CERTIFICATE_VERIFY_FAILED 与 Python3

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35569042/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 16:36:49  来源:igfitidea点击:

SSL: CERTIFICATE_VERIFY_FAILED with Python3

pythonpython-3.xmacosurllib

提问by PafflesWancakes

I apologize if this is a silly question, but I have been trying to teach myself how to use BeautifulSoup so that I can create a few projects.

如果这是一个愚蠢的问题,我深表歉意,但我一直在尝试自学如何使用 BeautifulSoup,以便我可以创建一些项目。

I was following this link as a tutorial: https://www.youtube.com/watch?v=5GzVNi0oTxQ

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

After following the exact same code as him, this is the error that I get:

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

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)

During handling of the above exception, another exception occurred:

在处理上述异常的过程中,又发生了一个异常:

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)>

Can someone help me figure out how to fix this?

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

回答by Bhavik

Go to the folder where Python is installed, e.g., in my case (Mac OS) it is installed in the Applications folder with the folder name 'Python 3.6'. Now double click on 'Install Certificates.command'. You will no longer face this error.

转到安装 Python 的文件夹,例如,在我的情况下 (Mac OS),它安装在文件夹名称为“Python 3.6”的 Applications 文件夹中。现在双击“安装 Certificates.command”。您将不再面临此错误。

回答by Jia

In my case, I used the sslmodule to "workaround" the certification like so:

就我而言,我使用该ssl模块来“解决”认证,如下所示:

import ssl

ssl._create_default_https_context = ssl._create_unverified_context

Then to read your link content, you can use:

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

urllib.request.urlopen(urllink)

回答by AAber

When you are using a self signed cert urllib3 version 1.25.3 refuses to ignore the SSL cert

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

To fix remove urllib3-1.25.3 and install urllib3-1.24.3

修复删除 urllib3-1.25.3 并安装 urllib3-1.24.3

pip3 uninstall urllib3

pip3 uninstall urllib3

pip3 install urllib3==1.24.3

pip3 install urllib3==1.24.3

Tested on Linux MacOS and Window$

在 Linux MacOS 和 Window$ 上测试

回答by Dan Walters

On Debian 9 I had to:

在 Debian 9 上,我必须:

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

I'm not sure why, but this enviroment variable was never set.

我不知道为什么,但从未设置过这个环境变量。

回答by Kostas Tsiligkiris

I had this problem in MacOS, and I solved it by linking the brew installed python 3 version, with

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

brew link python3

After that, it worked without a problem.

之后,它工作没有问题。