SSL:CERTIFICATE_VERIFY_FAILED 与 Python3 [重复]
- 2025-04-16 08:56:00
- admin 原创
- 17
问题描述:
如果这是一个愚蠢的问题,我很抱歉,但我一直在尝试自学如何使用 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)>
有人能帮我弄清楚如何解决这个问题吗?
解决方案 1:
就我而言,我使用该ssl
模块来“解决”认证问题,如下所示:
import ssl
ssl._create_default_https_context = ssl._create_unverified_context
然后要读取链接内容,您可以使用:
urllib.request.urlopen(urllink)
请注意,虽然这会使代码运行,但它会消除安全保护。
解决方案 2:
前往 Python 的安装文件夹,例如,在我的 Mac OS 系统中,它安装在“应用程序”文件夹中,文件夹名称为“Python 3.6”。现在双击“Install Certificates.command”。这样就不会再出现此错误了。
对于那些没有运行 Mac 或具有不同设置且找不到此文件的用户,该文件仅运行:
pip install --upgrade certifi
解决方案 3:
在 Debian 9 上我必须:
$ sudo update-ca-certificates --fresh
$ export SSL_CERT_DIR=/etc/ssl/certs
我不确定为什么,但这个环境变量从未被设置。
解决方案 4:
在 ssl 库的最新版本中,这一点已经发生了变化。SSLContext 被移到了它自己的属性中。这相当于 Jia 在 Python 3.8 中的回答。
import ssl
ssl.SSLContext.verify_mode = ssl.VerifyMode.CERT_OPTIONAL
解决方案 5:
我有一个使用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
瞧!
解决方案 6:
作为一种解决方法(不安全),您可以通过将 PYTHONHTTPSVERIFY 环境变量设置为 0 来关闭证书验证:
export PYTHONHTTPSVERIFY=0
解决方案 7:
在deltree 于 2021 年末对Jia 的 2018 年答案进行更新的基础上,我能够通过以下方式实现同等功能:
import urllib.request
import ssl
def urllib_get_2018():
# Using a protected member like this is not any more fragile
# than extending the class and using it. I would use it.
url = 'https://localhost:6667/my-endpoint'
ssl._create_default_https_context = ssl._create_unverified_context
with urllib.request.urlopen(url = url) as f:
print(f.read().decode('utf-8'))
def urllib_get_2022():
# Finally! Able to use the publice API. Happy happy!
url = 'https://localhost:6667/my-endpoint'
scontext = ssl.SSLContext(ssl.PROTOCOL_TLS)
scontext.verify_mode = ssl.VerifyMode.CERT_NONE
with urllib.request.urlopen(url = url, context=scontext) as f:
print(f.read().decode('utf-8'))
我需要使用CERT_NONE
而CERT_OPTIONAL
不是创建一个ssl.SSLContext(ssl.PROTOCOL_TLS)
来传递给urlopen
。
继续使用很重要,因为在处理可能尚未安装的urllib
小型容器镜像时它是有意义的。pip
解决方案 8:
当您使用自签名证书时,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$ 上测试
解决方案 9:
我在 Ubuntu 20.4 上遇到了同样的问题,尝试了很多解决方案,但都没用。最后我检查了 openssl 版本。即使更新升级后,openssl 版本仍然显示OpenSSL 1.1.1h [2020 年 9 月 22 日]。但在我的 Windows 系统中,代码运行正常,openssl 版本是OpenSSL 1.1.1k 2021 年 3 月 25 日。
我决定手动更新 openssl,结果成功了!感谢上帝!
步骤如下(Ubuntu 20.4):
*检查 openssl 版本
openssl version -a
*要更新 openssl:
sudo apt install build-essential checkinstall zlib1g-dev
cd /usr/local/src/
sudo wget https://www.openssl.org/source/openssl-1.1.1k.tar.gz
sudo tar -xf openssl-1.1.1k.tar.gz
cd openssl-1.1.1k
sudo ./config --prefix=/usr/local/ssl --openssldir=/usr/local/ssl shared zlib
sudo make
sudo make test
sudo make install
cd /etc/ld.so.conf.d/
sudo nano openssl-1.1.1k.conf
*输入/usr/local/ssl/lib并保存
sudo ldconfig -v
sudo nano /etc/environment
*将“:/usr/local/ssl/bin”添加到路径
source /etc/environment
echo $PATH
*现在检查 openssl 版本
openssl version -a
解决方案 10:
您可能会执行命令:pip install --upgrade certifi
或者您可能打开了 charles/fiddler,只需关闭它
解决方案 11:
我在 MacOS 中遇到了这个问题,我通过链接 brew 安装的 python 3 版本解决了这个问题,
brew link python3
此后,一切运行正常。
扫码咨询,免费领取项目管理大礼包!