在linux上安装python ssl模块而无需重新编译

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27952343/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 02:31:11  来源:igfitidea点击:

Install python ssl module on linux without recompiling

pythonsslopenssl

提问by Phalse

Is it possible to install the SSL module for python on a linux box that already has OpenSSL installed without recompiling python? I was hoping it would be as simple as copying over a few files and including them in the library path. Python version is 2.4.3. Thanks!

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

回答by VT_Drew

NOTE:Python >= 2.6 already has SSL support built-in, there's no need to install sslpackage.

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

Install package from pypi:

从pypi安装包:

pip install ssl

If you're missing pipcommand, install it for your distribution:

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

RedHat/Centos:

红帽/Centos:

yum install python-pip

Debian/Ubuntu

Debian/Ubuntu

apt-get install python-pip

回答by jww

Is it possible to install the SSL module for python on a linux box that already has OpenSSL installed without recompiling python?

是否可以在不重新编译 python 的情况下,在已经安装了 OpenSSL 的 linux 机器上安装 python 的 SSL 模块?

Yes. Python's setup.pyuses the following logic to detect OpenSSL:

是的。Pythonsetup.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']), )

The point is Python is notstatic linking against libssland libcrypto. (Some static linking occurs with cctyes, but nothing else).

关键是 Python不是针对libssl和 的静态链接libcrypto。(一些静态链接发生在cctyes,但没有别的)。

Now, the bad thing is that the project uses system paths beforeyour locally installed paths. For example, the project uses inc_dirs(system) beforesearch_for_ssl_incs_in(local). (See more on this below).

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

After you run configure, you will have a Modules/Setupwith the following lines commented out:

运行后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

Again, no static linking. (And this assumes the previous version of Python uncommented those lines).

同样,没有静态链接。(这假设以前版本的 Python 取消了这些行的注释)。

So you should be able to build a binary compatibleversion of OpenSSL and use LD_LIBRARY_PATHor LD_PREOLADto ensure Python uses your updated version of OpenSSL.

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

OpenSSL 0.9.7 and 0.9.8 are binary compatible. OpenSSL 1.0.0, 1.0.1 and 1.0.2 are binary compatible. OpenSSL 0.9.8 and 1.0.0 are notbinary compatible.

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

----------

----------

Here's the problem with Python's setup placing system includes beforelocal includes:

这是 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);
...

Python used the down level version 0.9.8 version of OpenSSL provided by Apple, and not my recent OpenSSL 1.0.1k. That's despite me (1) exporting them in CFLAGSand LDFLAGS; (2) editing Setup; and (3) editing Modules/Setup.

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

And I still have runtime path problems to contend with, so I'll need to use LD_PRELOAD_PATH, DYNLIB_LIBRARY_PATH, etc.

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