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

2024-06-01 22:16:39 发布

您现在位置:Python中文网/ 问答频道 /正文

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

在本例中,我发现最好的方法是在~/local中安装python及其依赖项。它通常只起作用,但这次Python未能构建SSL模块(请参阅下面的详细信息)。这是我在做什么的痕迹。

所以我下载了Python6的源代码,然后离开:

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

对日志的检查显示尚未编译ssl模块,但未提及原因(在生成或配置中未发生其他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,I./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

我已经好几年没这么做了,所以也许我忽略了一些事情。有人能帮忙解决这个问题吗?

提前谢谢。


Tags: ornohomelocalopenpiddirectoryfile
3条回答

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

工作原理是通过根据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处的堆栈溢出上使用静态链接的详细信息。

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

If you find yourself on a linux box needing ssl support in python (to use a client in things like httplib.HTTPSConnection or imaplib.IMAP4_SSL), then let me save you a couple of hours of hunting around the web (of course if you have found this then that means you've done some level hunting already!).

You'll know if you need ssl support compiled into your python installation if you get the following exception message: AttributeError: 'module' object has no attribute 'ssl'

In order to make that go away so you can continue happily slinging python code, you'll need to first make sure you have OpenSSL installed. By default it is installed from source at: /usr/local/ssl

If that directory doesn't exist, then grab the source package.

Do the standard:

tar zxf openssl-0.9.8g.tar.gz
cd openssl-0.9.8g
./config
make
make install

Then grab the python sources for 2.5.1 and: tar zxf Python-2.5.1.tgz && cd Python-2.5.1

Then you need to edit the 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

If you installed OpenSSL in the default locations you can just uncomment lines 206-209, then:

./configure
make
make install

Then verify your installation with:

python /usr/local/lib/python2.5/test/test_socket_ssl.py
test_rude_shutdown ...
test_basic ...
test_timeout ...

确保通过清理源根(例如make distclean)来获取对Modules/Setup.dist的更改,并再次运行configuremake

在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

相关问题 更多 >