python - 在非标准位置构建具有 SSL 支持的 Python

标签 python openssl compilation

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

在这种情况下,我发现最好的办法是在 ~/local 中安装 python 及其依赖项。它通常可以正常工作,但这次 Python 无法构建 SSL 模块(请参阅下面的详细信息)。这是我正在做的事情的痕迹。

所以我下载了 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 和 make。它失败了,但这次不同:

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

我已经很多年没有这样做了,所以也许我忽略了一些东西。谁能帮忙解决这个问题?

提前致谢。

最佳答案

如果 OpenSSL 不在标准位置,您需要编辑 Modules/Setup.dist 以指定 OpenSSL 的位置。来自 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)并运行 configure 来获取对 Modules/Setup.dist 的更改然后再次make

关于python - 在非标准位置构建具有 SSL 支持的 Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5937337/

相关文章:

c - 为什么指针导致非指针编译错误?

python - 在文本文件中查找值

ssl - 如何从 keystore.jks 和 truststore.jks 文件创建证书

linux - 无法使用sudo命令编译cuda

iPhone SDK Objective-C __DATE__ (编译日期)无法转换为 NSDate

c++ - 从文件加载 RSA 公钥时遇到问题

python - 在文件中搜索并用单词替换数字

python - 如何在 webdriver 运行时更改默认下载文件夹?

python - os.system 不工作,但在命令提示符下输入同样的东西可以工作

delphi - 我们如何连接网站?收到 SSL 错误 1409442E