首页 文章

在非标准位置使用SSL支持构建Python

提问于
浏览
47

我需要在RHEL上安装几个Python模块,我没有root访问权限 . 至少有一个模块还需要访问 Python.h .

在这种情况下,我发现最好的方法是在 ~/local 中安装python及其依赖项 . 它通常只是工作,但这次Python无法构建SSL模块(请参阅下面的详细信息) . 这里's the trace of what I'在做什么 .

所以我下载了python 6源码然后关闭了我:

./configure --prefix=/home/fds/rms/local
make >& make.log

对日志的检查显示ssl模块尚未编译,但没有提及原因(make或configure中没有其他ssl出现):

Failed to find the necessary bits to build these modules:
_bsddb             _curses            _curses_panel
_hashlib           _sqlite3           _ssl   <----------

所以我想,python根本找不到任何ssl库(这很奇怪,但是嘿......) . 所以我下载openssl-0.9.8r和

./config --prefix=/home/fds/rms/local shared
make
make install

现在回到Python,我./configure并重新制作 . 它失败了,但这一次有所不同:

Failed to build these modules:
_hashlib           _ssl

仔细检查日志文件可以看出:

gcc -pthread -shared build/temp.linux-x86_64-2.6/home/fds/rms/installers/Python-2.6.6/Modules/_ssl.o -L/home/fds/rms/local/lib -L/usr/local/lib -lssl -lcrypto -o build/lib.linux-x86_64-2.6/_ssl.so
*** WARNING: renaming "_ssl" since importing it failed: libssl.so.0.9.8: cannot open shared object file: No such file or directory

所以现在它正在拿起图书馆,但并没有完全正确(文件应该在哪里):

$ find /home/fds/rms/local -iname libssl.so.0.9.8
/home/fds/rms/local/lib/libssl.so.0.9.8

接下来就是跟踪make并查看它在哪里寻找文件:

$ strace -f make 2>&1 | grep libssl.so.0.9.8
[pid  5584] open("/lib/libssl.so.0.9.8", O_RDONLY) = -1 ENOENT (No such file or directory)
[pid  5584] open("/usr/lib/libssl.so.0.9.8", O_RDONLY) = -1 ENOENT (No such file or directory)
[pid  5584] open("/lib64/tls/x86_64/libssl.so.0.9.8", O_RDONLY) = -1 ENOENT (No such file or directory)
[pid  5584] open("/lib64/tls/libssl.so.0.9.8", O_RDONLY) = -1 ENOENT (No such file or directory)
[pid  5584] open("/lib64/x86_64/libssl.so.0.9.8", O_RDONLY) = -1 ENOENT (No such file or   directory)
[pid  5584] open("/lib64/libssl.so.0.9.8", O_RDONLY) = -1 ENOENT (No such file or directory)
[pid  5584] open("/usr/lib64/tls/x86_64/libssl.so.0.9.8", O_RDONLY) = -1 ENOENT (No such file or directory)
[pid  5584] open("/usr/lib64/tls/libssl.so.0.9.8", O_RDONLY) = -1 ENOENT (No such file or directory)
[pid  5584] open("/usr/lib64/x86_64/libssl.so.0.9.8", O_RDONLY) = -1 ENOENT (No such file or directory)
[pid  5584] open("/usr/lib64/libssl.so.0.9.8", O_RDONLY) = -1 ENOENT (No such file or directory)
[pid  5584] write(1, "*** WARNING: renaming \"_ssl\" sin"..., 131*** WARNING: renaming "_ssl" since importing it failed: libssl.so.0.9.8: cannot open shared object file: No such file or directory
[pid  5584] open("/lib/libssl.so.0.9.8", O_RDONLY) = -1 ENOENT (No such file or directory)
[pid  5584] open("/usr/lib/libssl.so.0.9.8", O_RDONLY) = -1 ENOENT (No such file or directory)
[pid  5584] open("/lib64/libssl.so.0.9.8", O_RDONLY) = -1 ENOENT (No such file or directory)
[pid  5584] open("/usr/lib64/tls/libssl.so.0.9.8", O_RDONLY) = -1 ENOENT (No such file or directory)
[pid  5584] open("/usr/lib64/libssl.so.0.9.8", O_RDONLY) = -1 ENOENT (No such file or directory)
[pid  5584] write(1, "*** WARNING: renaming \"_hashlib\""..., 135*** WARNING: renaming "_hashlib" since importing it failed: libssl.so.0.9.8: cannot open shared object file: No such file or directory

嗯,它正在寻找所有错误的地方 . 我试着给出一个提示:

CPPFLAGS="-I/home/fds/rms/local/include -I/home/fds/rms/local/include/openssl" LDFLAGS="-L/home/fds/rms/local/lib" ./configure --prefix=/home/fds/rms/local

但没有任何改变, make 似乎根本没有尝试 /home/fds/rms/local/lib .

多年来我没有这样做,所以也许我忽略了一些东西 . 有人可以帮忙解决这个问题吗?

提前致谢 .

9 回答

  • 4

    对我来说编辑 Modules/Setup 是不够的,因为 _hashlib 模块仍然使用错误的OpenSSL版本;在我的SLES系统上运行时没有考虑 LD_LIBRARY_PATH .

    通过根据GitHub patch: eddy-geek/ python_custom_openssl.diff编辑 setup.py 然后 make clean && make ,将本地OpenSSL静态链接到 _ssl_hashlib 的作用是什么 .

    有关我在Coredump when compiling python with a custom openssl version上使用Stack Overflow上的静态链接的原因的更多详细信息 .

  • 0

    对于 MAC OS HIGH SierraPython-3.5.6 使用brew安装openssl

    brew install openssl
    

    这将在/usr/local/Cellar/openssl/1.0.2o_2/上安装openssl . 此路径需要在Modules / Setup.dist中更新

    204:# Socket module helper for SSL support; you must comment out the other
    205:# socket line above, and possibly edit the SSL variable:
    206:SSL=/usr/local/ssl
    207:_ssl _ssl.c \
    208:    -DUSE_SSL -I$(SSL)/include -I$(SSL)/include/openssl \
    209:    -L$(SSL)/lib -lssl -lcrypto
    

    在上面的行中更新SSL值

    SSL=/usr/local/Cellar/openssl/1.0.2o_2/
    

    并取消注释行,做一个CMM,你的python将用openssl编译 .

  • 1

    尝试将 -Wl,-rpath,/home/fds/rms/local/lib 添加到 LDPATH .

  • 2

    我有一组静态openssl和静态libintl的补丁,下面是2和3 .

    对于openssl(第一个补丁),您必须设置env var OPENSSL_ROOT .

    这是基于http://gist.github.com/eddy-geek/9604982的补丁 .

    对于Python 2.7.14:

    https://gist.github.com/rkitover/2d9e5baff1f1cc4f2618dee53083bd35

    https://gist.github.com/rkitover/afab7ed3ac7ce1860c43a258571c8ae1

    对于Python 3.6.3:

    https://gist.github.com/rkitover/93d89a679705875c59275fb0a8f22b45

    https://gist.github.com/rkitover/b18f19eafda3775a9652cc9cdf3ec914

  • 0

    如果OpenSSL不在标准位置,则需要编辑 Modules/Setup.dist 以指定OpenSSL的位置 . 来自Getting SSL Support in Python 2.5.1

    如果你发现自己在一个需要ssl支持的linux盒子里(在httplib.HTTPSConnection或imaplib.IMAP4_SSL这样的东西中使用一个客户端),那么让我在网上拯救你几个小时(当然,如果你已经找到了这个意味着你已经完成了一些水平的狩猎!) . 如果你收到以下异常消息,你会知道你是否需要编译到你的python安装中的ssl支持:AttributeError:'module'对象没有属性'ssl'为了让它消失所以你可以继续愉快地投掷python代码,您需要先确保安装了OpenSSL . 默认情况下,它从源安装在:/ usr / local / ssl如果该目录不存在,则获取源包 . 做标准:tar zxf openssl-0.9.8g.tar.gz
    cd openssl-0.9.8g
    ./config中
    使
    make install
    然后获取2.5.1的python源代码:tar zxf Python-2.5.1.tgz && cd Python-2.5.1然后你需要编辑Modules / Setup.dist:204:#Socket模块助手以获得SSL支持;你必须注释掉另一个
    205:上面的#socket行,可能编辑SSL变量:
    206:SSL =的/ usr /本地/ SSL
    207:_ssl _ssl.c
    208:-DUSE_SSL -I $(SSL)/ include -I $(SSL)/ include / openssl
    209:-L $(SSL)/ lib -lssl -lcrypto
    如果您在默认位置安装了OpenSSL,则只需取消注释206-209行,然后:./ configure
    使
    make install
    然后使用以下命令验证安装:python /usr/local/lib/python2.5/test/test_socket_ssl.py
    test_rude_shutdown ...
    test_basic ......
    test_timeout ......

  • 51

    在Bourne shell中(/ bin / sh或/ bin / bash):

    $ LD_LIBRARY_PATH=/usr/local/lib
    $ export LD_LIBRARY_PATH
    $ make
    

    在C-shell中(/ bin / csh或/ bin / tcsh):

    % setenv LD_LIBRARY_PATH /usr/local/lib
    % make
    
  • 3

    这是我在Python 2.7.11中使用的完整过程 .


    在顶级Python2.7.11源目录中:

    • 更改 Modules/Setup.distModules/Setup :取消注释 _ssl 部分,注释掉 _socket (如果已经注释掉,则为no-op),取消注释并适当设置SSL(新ssl的路径 lib/includes 等)

    注意:模块/设置文件最初不存在,但是在第一次运行后,我从相信的Modules / Setup.dist获取内容 . 确保每次运行前都会在此处反映更改 .

    ./configure --prefix=/root/.local/aks/py-ssl/python2.7 --enable-shared
    
    # modify: Makefile -> set svnversion to ""
    
    make
    
    make altinstall
    
  • 2

    我正在构建Python2.7.13,我看到了同样的问题 . 对于2.7.13,您必须使用“openssl1.0.0e”或更高版本让它起作用 . 我试过openssl-0.9.8g并没有用 . 不知怎的,我不能让它只修改Modules / Setup.dist,所以我必须手动编译_ssl.o . 我想这是因为我提供的openssl-0.9.8g无法正常工作,它搜索了系统默认的libssl.so.10,它也不起作用 .

  • 6

    我得到了相同的结果,直到我回到openssl的日志 . 在那里我看到你需要在构建openssl时使用-fPIC:构建'_ssl'扩展:

    gcc -pthread -fPIC -fno-strict-aliasing -g -O2 -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -I/usr/local/ssl/include -I. -IInclude -I./Include -I/usr/local/include -I/home/feramos/Python-2.7.3/Include -I/home/feramos/Python-2.7.3 -c /home/feramos/Python-2.7.3/Modules/_ssl.c -o build/temp.linux-x86_64-2.7/home/feramos/Python-2.7.3/Modules/_ssl.o
    gcc -pthread -shared build/temp.linux-x86_64-2.7/home/feramos/Python-2.7.3/Modules/_ssl.o -L/usr/local/ssl/lib -L/usr/local/lib -lssl -lcrypto -o build/lib.linux-x86_64-2.7/_ssl.so
    /usr/bin/ld: /usr/local/ssl/lib/libcrypto.a(x86_64cpuid.o): relocation R_X86_64_PC32 against `OPENSSL_cpuid_setup' can not be used when making a shared object; recompile with -fPIC
    
    openssl-0.9.8g]# .config -fPIC
    

    然后,make,make为openssl安装,然后再次构建Python .

相关问题