Python3添加支持ssl模块
· 技术积累 · Python Linux openssl

在编译安装python3时候如果没有安装openssl,编译安装后使用就会报错ImportError: No module named _ssl

openssl-1.0.* 和 1.1.*版本, 二选一安装

openssl-1.0.2版本

#查看默认的openssl版本比较老了
[root@localhost ~]# openssl version
OpenSSL 1.0.2k-fips  26 Jan 2017

#安装openssl-1.0.2u版本
[root@localhost ~]# wget https://www.openssl.org/source/old/1.0.2/openssl-1.0.2u.tar.gz
[root@localhost ~]# tar xf openssl-1.0.2u.tar.gz 
[root@localhost ~]# cd openssl-1.0.2u
[root@localhost openssl-1.0.2u]# ./config
[root@localhost openssl-1.0.2u]# make && make install

# python添加ssl支持编译时候可能出现的错误:/usr/local/ssl/lib/libssl.a: error adding symbols: Bad value。
# 请在openssl编译安装config时加-fPIC参数

# 在生成一个动态库时需要指定-fPIC,这是创建动态库所要求的,共享库被加载是在内存中的位置是不固定的,是一个相对的位置。
# 那么在生成静态库时通常不指定-fPIC, 可是在64bit编译使用静态库就会提示需要-fPIC重新编译该库。由于openssl编译静态库
# 时,没有使用-fPIC选项,使得编译出来的静态库没有重定位能力。这样在64bit机器上编译出来的静态库如果不指定-fPIC选项几乎
# 全部不能使用。因此需要重新加上-fPIC从新编译openssl

#配置新版本生效(记得修改路径)
[root@localhost ~]# echo 'export PATH=$PATH:/usr/local/ssl/bin' >> /etc/profile && source /etc/profile

#配置 openssl 共享库
echo '/usr/local/ssl/lib' >>/etc/ld.so.conf && ldconfig

[root@localhost ~]# which openssl
/usr/local/ssl/bin/openssl
[root@localhost ~]# openssl version
OpenSSL 1.0.2u  20 Dec 2019

openssl-1.1*版本

#安装使用openssl-1.1.1g版本
wget https://www.openssl.org/source/openssl-1.1.1g.tar.gz
tar xf openssl-1.1.1g.tar.gz 
cd openssl-1.1.1g
./config  --prefix=/usr/local/openssl-1.1.1g
make && make install

#配置新版本生效(记得替换路径,我这里用*表示)
[root@localhost ~]# echo 'export PATH=$PATH:/usr/local/openssl-1.1.*/bin' >> /etc/profile && source /etc/profile

#配置 openssl 共享库路径(记得替换路径,我这里用*表示)
echo '/usr/local/openssl-1.1.*/lib' >>/etc/ld.so.conf && ldconfig

重新安装python3

#只需要将python的安装目录删除即可
[root@localhost ~]# rm -rf /usr/local/python38/

#重新解压原来的源码包
[root@localhost ~]# tar xf Python-3.8.2.tar.tgz
[root@localhost ~]# cd Python-3.8.2

###configure注意
#如果使用openssl-1.0.*的版本,configure配置:
[root@localhost Python-3.8.2]# vim Modules/Setup #删除ssl的注释
······
# Socket module helper for SSL support; you must comment out the other
# socket line above, and possibly edit the SSL variable:
SSL=/usr/local/ssl
_ssl _ssl.c \
        -DUSE_SSL -I$(SSL)/include -I$(SSL)/include/openssl \
        -L$(SSL)/lib -lssl -lcrypto

[root@localhost Python-3.8.2]# ./configure --prefix=/usr/local/python38 --enable-shared


#如果使用openssl-1.1.*的版本,configure配置:
[root@localhost Python-3.8.2]# ./configure --prefix=/usr/local/python38 --enable-shared --with-openssl=/usr/local/openssl-1.1.*

#编译/安装
[root@localhost Python-3.8.2]# make
[root@localhost Python-3.8.2]# make install

Python3添加支持ssl模块

Python3添加支持ssl模块


python3编译报错:

#如果安装1.1.0还有1.1.1版本,config没有指定安装目录python编译的时候会报错,要么就是make的最后报下面的错
Following modules built successfully but were removed because they could not be imported:
_hashlib              _ssl                                     


Could not build the ssl module!
Python requires an OpenSSL 1.0.2 or 1.1 compatible libssl with X509_VERIFY_PARAM_set1_host().
LibreSSL 2.6.4 and earlier do not provide the necessary APIs, https://github.com/libressl-portable/portable/issues/381

查看ssl模块是否支持

#确认ssl模块
[root@localhost Python-3.8.2]# python3
Python 3.8.2 (default, May 16 2020, 03:09:38) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import ssl
>>>

Python3添加支持ssl模块


本文最后更新时间 2024-03-30
文章链接地址:
https://me.jinchuang.org/archives/584.html
本站文章除注明[转载|引用],均为本站原创内容,转载前请注明出处
使用腾讯云CDN的SDK 用脚本去刷新URL缓存
关于网站搬家后,评论邮件通知发送失败报错的问题(openssl的问题)
Ubuntu 22.04 源码编译安装 PHP7.4 使用OpenSSL1.1.1版本
统计文本中每个字符出现的次数

Centos离线安装Nginx

centos7 安装使用virtualenvwrapper

我要留言