在Linux上安装python ssl模块而不重新编译

Pha*_*lse 10 python ssl openssl

是否可以在已经安装了OpenSSL而不重新编译python的linux机器上安装python的SSL模块?我希望它就像复制几个文件并将它们包含在库路径中一样简单.Python版本是2.4.3.谢谢!

jww*_*jww 5

是否可以在已经安装了OpenSSL的Linux机器上安装python的SSL模块,而无需重新编译python?

是。Python setup.py使用以下逻辑来检测OpenSSL:

search_for_ssl_incs_in = [
                      '/usr/local/ssl/include',
                      '/usr/contrib/ssl/include/'
                     ]

ssl_incs = find_file('openssl/ssl.h', inc_dirs,
                     search_for_ssl_incs_in

ssl_libs = find_library_file(self.compiler, 'ssl',lib_dirs,
                             ['/usr/local/ssl/lib',
                              '/usr/contrib/ssl/lib/'
                             ] )

if (ssl_incs is not None and
    ssl_libs is not None):
    exts.append( Extension('_ssl', ['_ssl.c'],
                           include_dirs = ssl_incs,
                           library_dirs = ssl_libs,
                           libraries = ['ssl', 'crypto'],
                           depends = ['socketmodule.h']), )
Run Code Online (Sandbox Code Playgroud)

关键是Python 不是静态链接到libssl和的libcrypto。(某些静态链接与发生cctyes,但没有其他关联)。

现在,不好的是项目本地安装的路径之前使用了系统路径。例如,项目(本地)之前使用inc_dirs(系统)。(请参阅下面的更多内容)。 search_for_ssl_incs_in

运行后configure,您将看到Modules/Setup带有以下注释的行:

# 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
Run Code Online (Sandbox Code Playgroud)

同样,没有静态链接。(并且假设Python的先前版本未注释这些行)。

因此,您应该能够构建OpenSSL 的二进制兼容版本,并使用LD_LIBRARY_PATHLD_PREOLAD确保Python使用更新的OpenSSL版本。

OpenSSL 0.9.7和0.9.8是二进制兼容的。OpenSSL 1.0.0、1.0.1和1.0.2是二进制兼容的。OpenSSL 0.9.8和1.0.0 二进制兼容。

----------

这是Python的安装程序放置系统本地包含之前包含的问题:

export CFLAGS="-I/usr/local/ssl/darwin/include"; export LDFLAGS="-L/usr/local/ssl/darwin/lib"
<edit Setup search_for_ssl_incs_in and search_for_ssl_incs_in>
./configure
<edit Modules/Setup>
make
...
/Users/jww/Python-3.4.2/Modules/_ssl.c:390:9: warning: 
      'ERR_peek_last_error' is deprecated [-Wdeprecated-declarations]
    e = ERR_peek_last_error();
        ^
/usr/include/openssl/err.h:274:15: note: 'ERR_peek_last_error' declared here
unsigned long ERR_peek_last_error(void) DEPRECATED_IN_MAC_OS_X_VERSION_1...
              ^
/Users/jww/Python-3.4.2/Modules/_ssl.c:393:15: warning: 
      'SSL_get_error' is deprecated [-Wdeprecated-declarations]
        err = SSL_get_error(obj->ssl, ret);
...
Run Code Online (Sandbox Code Playgroud)

Python使用Apple提供的OpenSSL的下层版本0.9.8,而不是我最近的OpenSSL 1.0.1k。尽管有我(1)将它们导出到CFLAGS和中LDFLAGS;(2)编辑Setup;和(3)编辑Modules/Setup

我仍然有运行时路径问题与之抗衡,所以我需要使用LD_PRELOAD_PATHDYNLIB_LIBRARY_PATH等等。


VT_*_*rew 3

注意: Python >= 2.6 已经内置了 SSL 支持,无需安装ssl包。

从 pypi 安装包:

pip install ssl
Run Code Online (Sandbox Code Playgroud)

如果您缺少pip命令,请为您的发行版安装它:

红帽/Centos:

yum install python-pip
Run Code Online (Sandbox Code Playgroud)

Debian/Ubuntu

apt-get install python-pip
Run Code Online (Sandbox Code Playgroud)

  • 这是没用的,这就是当你运行 `pip install ssl` 时发生的情况 `pip 配置了需要 TLS/SSL 的位置,但是 Python 中的 ssl 模块不可用。` 令人震惊... (8认同)