Python 安装依赖包,出现 ssl.SSLCertVerificationError 的问题,解决方法
问题描述
CentOS7环境,已安装 Python3.8.10。
最近项目开发,需要切换 solidity 版本,参考开发文档,需要执行如下命令
pip install solc-select==0.2.0
# 安装指定版本solc
solc-select install <solc版本号>
# 切换solc版本
solc-select use <solc版本号>
先使用 pip install solc-select==0.2.0
命令成功安装了 solc-select
,然后执行 solc-select install
查看有哪些可用的 solc
版本,遇到如下问题:
$ solc-select install
Available versions to install:
Traceback (most recent call last):
File "/usr/local/python3/python3.8.10/lib/python3.8/urllib/request.py", line 1354, in do_open
h.request(req.get_method(), req.selector, req.data, headers,
File "/usr/local/python3/python3.8.10/lib/python3.8/http/client.py", line 1252, in request
self._send_request(method, url, body, headers, encode_chunked)
File "/usr/local/python3/python3.8.10/lib/python3.8/http/client.py", line 1298, in _send_request
self.endheaders(body, encode_chunked=encode_chunked)
File "/usr/local/python3/python3.8.10/lib/python3.8/http/client.py", line 1247, in endheaders
self._send_output(message_body, encode_chunked=encode_chunked)
File "/usr/local/python3/python3.8.10/lib/python3.8/http/client.py", line 1007, in _send_output
self.send(msg)
File "/usr/local/python3/python3.8.10/lib/python3.8/http/client.py", line 947, in send
self.connect()
File "/usr/local/python3/python3.8.10/lib/python3.8/http/client.py", line 1421, in connect
self.sock = self._context.wrap_socket(self.sock,
File "/usr/local/python3/python3.8.10/lib/python3.8/ssl.py", line 500, in wrap_socket
return self.sslsocket_class._create(
File "/usr/local/python3/python3.8.10/lib/python3.8/ssl.py", line 1040, in _create
self.do_handshake()
File "/usr/local/python3/python3.8.10/lib/python3.8/ssl.py", line 1309, in do_handshake
self._sslobj.do_handshake()
ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1131)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/buaa/.local/bin/solc-select", line 8, in <module>
sys.exit(solc_select())
File "/home/buaa/.local/lib/python3.8/site-packages/solc_select/__main__.py", line 24, in solc_select
for version in sorted(get_installable_versions()):
File "/home/buaa/.local/lib/python3.8/site-packages/solc_select/solc_select.py", line 78, in get_installable_versions
return set(get_available_versions()) - set(installed_versions())
File "/home/buaa/.local/lib/python3.8/site-packages/solc_select/solc_select.py", line 82, in get_available_versions
list_json = urllib.request.urlopen(url).read()
File "/usr/local/python3/python3.8.10/lib/python3.8/urllib/request.py", line 222, in urlopen
return opener.open(url, data, timeout)
File "/usr/local/python3/python3.8.10/lib/python3.8/urllib/request.py", line 525, in open
response = self._open(req, data)
File "/usr/local/python3/python3.8.10/lib/python3.8/urllib/request.py", line 542, in _open
result = self._call_chain(self.handle_open, protocol, protocol +
File "/usr/local/python3/python3.8.10/lib/python3.8/urllib/request.py", line 502, in _call_chain
result = func(*args)
File "/usr/local/python3/python3.8.10/lib/python3.8/urllib/request.py", line 1397, in https_open
return self.do_open(http.client.HTTPSConnection, req,
File "/usr/local/python3/python3.8.10/lib/python3.8/urllib/request.py", line 1357, in do_open
raise URLError(err)
urllib.error.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1131)>
解决办法
网上搜索 'ssl.SSLCertVerificationError',看到帖子:
《python爬虫ssl.SSLCertVerificationError解决办法》
根据其中的
方法二:
全局取消证书验证,简易方便,但对整个项目可能会出现安全性的相关问题。
import ssl ssl._create_default_https_context = ssl._create_unverified_context
联想到可以手动修改 Python 中的 ssl 模块。
于是进入 Python3.8.10 的安装目录,搜索 ssl.py,打开该文件,手动修改之
$ cd /usr/local/python3/python3.8.10
$ find . -name ssl.py
./lib/python3.8/ssl.py
$ sudo vim ./lib/python3.8/ssl.py
找到
_create_default_https_context = create_default_context
这一行,注释掉,然后修改为
_create_default_https_context = _create_unverified_context
再次执行 solc-select install,就正常了:
$ solc-select install
Available versions to install:
0.4.10
0.4.11
0.4.12
0.4.13
0.4.14
0.4.15
0.4.16
0.4.17
0.4.18
0.4.19
0.4.20
0.4.21
0.4.22
0.4.23
0.4.24
0.4.25
0.4.26
0.5.0
0.5.1
0.5.10
0.5.11
0.5.12
0.5.13
0.5.14
0.5.15
0.5.16
0.5.17
0.5.2
0.5.3
0.5.4
0.5.5
0.5.6
0.5.7
0.5.8
0.5.9
0.6.0
0.6.1
0.6.10
0.6.11
0.6.12
0.6.2
0.6.3
0.6.4
0.6.5
0.6.6
0.6.7
0.6.8
0.6.9
0.7.0
0.7.1
0.7.2
0.7.3
0.7.4
0.7.5
0.7.6
0.8.0
0.8.1
0.8.10
0.8.11
0.8.12
0.8.13
0.8.14
0.8.15
0.8.16
0.8.17
0.8.18
0.8.19
0.8.2
0.8.20
0.8.3
0.8.4
0.8.5
0.8.6
0.8.7
0.8.8
0.8.9
注意事项
_create_default_https_context = _create_unverified_context
这一行代码的作用是取消 python 访问 https 服务器时的证书验证步骤,这是存在安全风险的;自己临时需要做的工作完成以后,就应该将 ssl.py 修改回原来的样子。
2023.06.14 Wed 14:45