手动安装Python3.10.8后Requests/Urllib3 HTTPS请求因SSL模块缺失失败求助
问题:手动安装Python 3.10.8后执行HTTPS GET请求提示SSL模块不可用
环境信息
- 系统:Red Hat 7.9 Maipo Linux
- Python版本:手动编译安装的3.10.8
- 已安装OpenSSL版本:
[root@epnm-lab-02-ha-01 ~]# rpm -qa|grep ^openssl* openssl-1.0.2k-21.el7_9.x86_64 openssl-libs-1.0.2k-21.el7_9.x86_64
报错信息
执行HTTPS请求时触发SSL错误:
[root@epnm-lab ~]# python3 Python 3.10.8 (main, Oct 14 2022, 02:15:05) [GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import requests >>> username = "xxx" >>> password = "yyy" >>> ip = "zzz" >>> requests.get(f"https://{username}:{password}@{ip}/webacs/api/v4/data/Devices?.full=true", verify=False) Traceback (most recent call last): File "/usr/local/lib/python3.10/site-packages/urllib3/connectionpool.py", line 692, in urlopen conn = self._get_conn(timeout=pool_timeout) File "/usr/local/lib/python3.10/site-packages/urllib3/connectionpool.py", line 281, in _get_conn return conn or self._new_conn() File "/usr/local/lib/python3.10/site-packages/urllib3/connectionpool.py", line 1011, in _new_conn raise SSLError( urllib3.exceptions.SSLError: Can't connect to HTTPS URL because the SSL module is not available. During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/usr/local/lib/python3.10/site-packages/requests/adapters.py", line 489, in send resp = conn.urlopen( File "/usr/local/lib/python3.10/site-packages/urllib3/connectionpool.py", line 787, in urlopen retries = retries.increment( File "/usr/local/lib/python3.10/site-packages/urllib3/util/retry.py", line 592, in increment raise MaxRetryError(_pool, url, error or ResponseError(cause)) urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='10.88.242.12', port=443): Max retries exceeded with url: /webacs/api/v4/data/Devices?.full=true (Caused by SSLError("Can't connect to HTTPS URL because the SSL module is not available")) During handling of the above exception, another exception occurred: Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/local/lib/python3.10/site-packages/requests/api.py", line 73, in get return request("get", url, params=params, **kwargs) File "/usr/local/lib/python3.10/site-packages/requests/api.py", line 59, in request return session.request(method=method, url=url, **kwargs) File "/usr/local/lib/python3.10/site-packages/requests/sessions.py", line 587, in request resp = self.send(prep, **send_kwargs) File "/usr/local/lib/python3.10/site-packages/requests/sessions.py", line 701, in send r = adapter.send(request, **kwargs) File "/usr/local/lib/python3.10/site-packages/requests/adapters.py", line 563, in send raise SSLError(e, request=request) requests.exceptions.SSLError: HTTPSConnectionPool(host='10.88.242.12', port=443): Max retries exceeded with url: /webacs/api/v4/data/Devices?.full=true (Caused by SSLError("Can't connect to HTTPS URL because the SSL module is not available.")) >>> [root@epnm-lab ~]#
问题原因
手动编译Python时,缺少OpenSSL开发依赖包,导致Python的SSL模块未被编译安装。系统中仅安装了OpenSSL运行时库,缺少编译所需的头文件和静态库。
解决方案
1. 安装编译依赖
先安装OpenSSL开发包及Python编译所需的其他依赖:
yum install -y openssl-devel gcc zlib-devel bzip2-devel readline-devel sqlite-devel libffi-devel
2. 重新编译Python 3.10.8
进入Python源码目录(如果已删除,需重新解压源码包),执行以下步骤:
# 清理之前的编译残留 make clean # 配置编译选项,确保启用SSL支持 ./configure --prefix=/usr/local/python3.10 --with-openssl=/usr --enable-optimizations # 编译并安装 make -j$(nproc) make altinstall
说明:
--prefix指定Python安装路径,--with-openssl=/usr指定系统OpenSSL的安装路径,--enable-optimizations启用编译优化。
3. 验证SSL模块是否正常
重新打开Python终端,验证SSL模块:
import ssl print(ssl.OPENSSL_VERSION)
如果能正常输出OpenSSL版本号,说明SSL模块已正确安装,此时再执行之前的HTTPS请求即可正常运行。
内容的提问来源于stack exchange,提问作者Luiz Polli
相关产品推荐
相关产品推荐

