【问题标题】:Install python ssl module on linux without recompiling在linux上安装python ssl模块而不重新编译
【发布时间】:2023-03-04 09:48:01
【问题描述】:

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

【问题讨论】:

    标签: python ssl openssl


    【解决方案1】:

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

    从 pypi 安装包:

    pip install ssl
    

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

    红帽/Centos:

    yum install python-pip
    

    Debian/Ubuntu

    apt-get install python-pip
    

    【讨论】:

    • 我使用的ubuntu盒子没有联网。
    • 您可以在机器上使用 U 盘/外置驱动器吗?如果是这样,只需将 .tar.gz 文件下载到另一台机器上的 USB 记忆棒(可以访问互联网)然后将 USB 记忆棒弹出到 ubuntu 框中并执行“pip install /home/username/ssl-1.16.tar.gz ”。显然您需要将 /home/username 替换为文件的路径。
    • 这个版本的linux没有安装pip,可以用yum做类似的吗?
    • 这没用,这就是你运行pip install sslpip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.Shocker时发生的...
    【解决方案2】:

    是否可以在已经安装 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']), )
    

    重点是 Python 不是针对libssllibcrypto 的静态链接。 (一些静态链接发生在 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
    

    同样,没有静态链接。 (这假设以前版本的 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 设置放置系统的问题,包括 before 本地包括:

    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 使用了 Apple 提供的 OpenSSL 的低版本 0.9.8 版本,而不是我最近的 OpenSSL 1.0.1k。尽管我(1)在CFLAGSLDFLAGS中导出它们; (2)编辑Setup; (3) 编辑Modules/Setup

    而且我还有运行时路径问题需要解决,所以我需要使用LD_PRELOAD_PATHDYNLIB_LIBRARY_PATH 等。

    【讨论】:

    • 这个不需要python的源码吗?我试图在没有来源的情况下做到这一点。你可能已经解释过了,但我误解了。
    • 看来python没有其他办法不编译就可以使用ssl了。
    • @Hassek - 是和否。如果您使用二进制兼容的 OpenSSL 0.9.8,那么不,不需要重新编译 Python。但是,OpenSSL 0.9.8 已停产 (EOL),它缺少大多数 ECC 和 TLS 1.2,因此您可能希望避免使用它。 OpenSSL 1.0.2 和 1.1.0 是继续的方式,如果是,您必须重新编译 Python。一旦您知道在哪里寻找 SSL 位(如上所示),就很容易重新编译。
    猜你喜欢
    • 2017-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-18
    相关资源
    最近更新 更多