【问题标题】:Updating openssl in python 2.7在 python 2.7 中更新 openssl
【发布时间】:2013-09-16 03:21:34
【问题描述】:

想知道是否有人可以解释openssl在python2.7中的工作原理。 我不确定python是否有自己的openssl或从本地机器/环境中获取它?

让我解释一下: (如果我在 Python 中这样做)

>>> import ssl
>>> ssl.OPENSSL_VERSION
'OpenSSL 0.9.8x 10 May 2012'

(在终端中)

$ openssl version
OpenSSL 0.9.8x 10 May 2012
$ which openssl 
/usr/bin/openssl

现在我更新了 openssl(已下载。)

$ cd openssl-1.0.1c
$ ./Configure darwin64-x86_64-cc --prefix=/usr --openssldir=/opt/local/etc/openssl shared
$ make
$ sudo make install

这创建了单独的导演(如指定),所以我将它复制到旧路径

cp -f /usr/local/ssl/bin/openssl /usr/bin/openssl

现在终端中的 openssl 版本已经更新,但不是来自 python!

$ openssl version
OpenSSL 1.0.1c 10 May 2012

我确实注意到 .dylib 仍然指向旧版本,我该如何更改?

$ ls -l /usr/lib/*ssl*
-rwxr-xr-x  1 root  wheel  411680 Jul 17  2012 /usr/lib/libssl.0.9.7.dylib
-rwxr-xr-x  1 root  wheel  602800 May 24 03:43 /usr/lib/libssl.0.9.8.dylib
-rwxr-xr-x  1 root  wheel  390908 Sep  9 17:37 /usr/lib/libssl.1.0.0.dylib
lrwxr-xr-x  1 root  wheel      18 Jul 17  2012 /usr/lib/libssl.dylib -> libssl.0.9.8.dylib

更新:我更改了链接,python 上仍然是旧版本。

$ ls -l /usr/lib/*ssl*
-rwxr-xr-x  1 root  wheel  411680 Jul 17  2012 /usr/lib/libssl.0.9.7.dylib
-rwxr-xr-x  1 root  wheel  602800 May 24 03:43 /usr/lib/libssl.0.9.8.dylib
-rwxr-xr-x  1 root  wheel  390908 Sep  9 17:37 /usr/lib/libssl.1.0.0.dylib
lrwxr-xr-x  1 root  wheel      18 Sep 11 15:47 /usr/lib/libssl.dylib -> libssl.1.0.0.dylib

【问题讨论】:

  • 不要使用安装前缀 / 或 /usr 或符号链接/复制系统 OpenSSL 来覆盖系统 OpenSSL,否则会导致巨大的问题。你会破坏你的系统。

标签: python ssl openssl


【解决方案1】:

过时的 SSL 是多个平台上的常见问题:

这是一般的方法...

0。安装 OpenSSL

  • 选项 I: 安装并行 OpenSSL 1.x 库(-dev 或 -devel)包的系统包。

    # FreeBSD
    
    pkg install openssl
    OPENSSL_ROOT=/usr/local
    
    
    # Mac (brew)
    
    brew install openssl # DO NOT DO ANY WEIRD SYMLINK HACKS, ITS KEG-ONLY FOR A REASON!
    OPENSSL_ROOT="$(brew --prefix openssl)"
    
  • 选项二:从源代码安装 OpenSSL 到一个临时目录

    OPENSSL_ROOT="$HOME/.build/openssl-1.0.1e"
    
    curl http://www.openssl.org/source/openssl-1.0.1e.tar.gz | tar zxvf -
    cd openssl-1.0.1e
    mkdir -p "$OPENSSL_ROOT"
    ./config no-hw --prefix="$OPENSSL_ROOT" --openssldir=...
    # osx (instead of previous line): ./Configure darwin64-x86_64-cc no-hw --prefix="$OPENSSL_ROOT" --openssldir=...
    make install
    cd ..
    rm -rf openssl-1.0.1e
    

1。 从源代码构建 Python

  • 选项A:使用pyenv:

    export CONFIGURE_OPTS="CPPFLAGS=-I"$OPENSSL_ROOT"/include LDFLAGS=-L"$OPENSSL_ROOT"/lib [your other options here]"
    pyenv install 2.7.6
    
  • 选项 B:从源代码安装 Python

    ./configure CPPFLAGS="-I$OPENSSL_ROOT/include" LDFLAGS="-L$OPENSSL_ROOT/lib" [your other options here]`
    make
    # ...
    # if compiled openssl was used, it can be safely deleted because python's module ssl links openssl statically.
    

示例:FreeBSD 9.2(跳过 make install 用于演示目的)

pkg install openssl curl gmake gdbm sqlite3 readline ncurses
OPENSSL_ROOT=/usr/local
curl http://www.python.org/ftp/python/2.7.6/Python-2.7.6.tar.xz | tar jxvf -
cd Python-2.7.6
./configure CPPFLAGS="-I$OPENSSL_ROOT/include" LDFLAGS="-L$OPENSSL_ROOT/lib" [your other options here]
make
./python -c 'import ssl; print(ssl.OPENSSL_VERSION)' # osx: ./python.exe ...
# prints: OpenSSL 1.0.1e 11 Feb 2013

之后,不再需要临时的 openssl 库 b/c 将带有 openssl 的 ssl 模型静态放入 python 可执行文件(使用 otool 或 readelf 进行验证)。

【讨论】:

  • 我在stackoverflow.com/questions/46457404/… 中遇到了同样的问题,您的回答对我有用。这仍然是 2017 年末的正确答案。您需要将 CPPFLAGS 和 LDFLAGS 与 ./configure 命令一起传递;你不能在环境中设置它们,否则 setup.py 不会注意到它们。
  • 如果唯一需要更新的是openssl,谁能向我解释为什么我必须重建/重新安装整个python?
  • @normanius 如果二进制文件静态链接 OpenSSL 之类的库,链接器会将其合并到二进制文件中,并且不能直接换出。如果它改为动态链接到共享库,有时只能更新库并获取新代码......但这必须非常小心。编译 OpenSSL 的目的是获取它的更新版本。如果只升级动态链接二进制文件的 OpenSSL,则可以在旧版本之上自定义编译新版本的 OpenSSL,而无需重新编译 Python。 Python 还必须配置--enable-shared。
【解决方案2】:

请参考http://rkulla.blogspot.kr/2014/03/the-path-to-homebrew.html

在 MAC 上通过 homebrew 将 openssl 升级到 1.0.1j 后,系统 python 仍然引用旧版本 0.9.8。原来python指的是openssl。所以我用 brewed openssl 安装了新的 python 并在 Mac 上完成了这个问题,而不是 Ubuntu。

在Mac OS X 10.10版和系统python 2.7.6版上,我的程序如下:

$ brew update

$ brew install openssl

然后就可以看到openssl 1.0.1j版本了。

$ brew link openssl --force 

$ brew install python --with-brewed-openssl    

您必须使用 brewed openssl 安装新的 python。然后,您可以看到 /usr/local/Cellar/python/2.7.8_2/bin/python。

$ sudo ln -s /usr/local/Cellar/python/2.7.8_2/bin/python /usr/local/bin/python

当然,/usr/local/* 应该由 $USER 拥有,而不是由 Ryan 告诉的 root,但我使用了 'sudo'。而且,在这条指令之前,我没有 /usr/local/bin/python。在此指令之后,您可以使用 python 版本 2.7.8 而不是 2.7.6。

最后,你可以看到如下;

$ python --version  
Python 2.7.8

$ python -c "import ssl; print ssl.OPENSSL_VERSION"
OpenSSL 1.0.1j 15 Oct 2014

到目前为止,我正在 Ubuntu 12.04 上开发它。如果我有适用于 Ubuntu 12.04 的解决方案,那么我将更新我的答案。我希望这个过程对你有所帮助。

【讨论】:

  • 尽管我的做法略有不同,但它仍然有效。我的步骤:$ brew update && brew install openssl,跳过链接:brew update python --with-brewed-openssl我已经用brew安装了python$ /usr/local/Cellar/python/2.7.13_1/bin/python2 -c "import ssl; print ssl.OPENSSL_VERSION"OpenSSL 1.0.2l 25 May 2017系统版本:OpenSSL 0.9.8zh 14 Jan 2016
  • 我得到... curl: (23) 写入正文失败 (0 != 16384) 尝试镜像... ==> 正在下载dl.bintray.com/homebrew/mirror/pkg-config-0.29.2.tar.gz 警告:创建文件失败警告:/用户/paulkenjora/Library/Caches/Homebrew/pkg-config-0.29.2.tar.gz.in 警告:完成:权限被拒绝
  • 在 Kivy 而不是 Python 上升级 OpenSSL 有什么等价物,比如“$ brew install python --with-brewed-openssl”?
【解决方案3】:

这可能是因为 Python 版本过时。

在 Python 2.7.1 上运行 python -c "import ssl; print ssl.OPENSSL_VERSION" 后,我看到我有这个过时的版本:OpenSSL 0.9.7l 28 Sep 2006。

似乎我的 Python 版本依赖于已弃用的 OpenSSL 版本,如以下所示 this forum:

对于即将发布的 Python 2.7.9 版本 (计划在 12 月初),我打算在 python.org OS X 安装程序使用他们自己的 OpenSSL 版本,因此没有 不再依赖于现已弃用的系统 OpenSSL。

我更新到 Python 2.7.9,问题立即得到解决。现在,在运行python -c "import ssl; print ssl.OPENSSL_VERSION" 之后,我得到了OpenSSL 0.9.8za 5 Jun 2014。

【讨论】:

    【解决方案4】:

    没有解决任何问题,以上方法都不适合我。我最终采取了一种更简单且不复杂的方法....

    1. 从官网安装python 2.7.13,它实际上安装为默认python,自动升级旧的python系统范围(是的!)。

    https://www.python.org/downloads/mac-osx/

    1. 安装 python 后升级 openssl。为系统 python 更新它(是的!)。

    sudo pip install --upgrade pyOpenSSL

    1. 您将不得不重新安装所有 python 模块(因为您替换了 python),我强烈建议使用 pip。在 pip 安装几分钟后,我的默认 OSX python 升级了,我升级了 openssl,我的所有模块(包括 django 正在运行)。

    【讨论】:

    • 请不要这样做。在 RHEL 之类的东西上替换系统 python 可能会产生一些不良后果。还有许多其他工具可用于通过不同版本的 OpenSSL 替代安装不同版本的 Python。 Conda 就是这种工具的一个例子。
    【解决方案5】:

    以下内容对我有用。我已经能够将 OpenSSL 从 0.9.8zh 更新到 1.0.2o 版本,但 python 从未访问过较新的版本,直到找到使用 pyenv 重新安装 python 的建议(使用 2.7.10,我想要的版本)。

    brew update
    brew install pyenv
    
    echo 'eval "$(pyenv init -)"' >> .bashrc
    source .bashrc
    
    pyenv install 2.7.10
    pyenv global 2.7.10
    

    然后检查...

    python --version
    Python 2.7.10
    
    python -c 'import ssl; print ssl.OPENSSL_VERSION'
    OpenSSL 1.0.2o  27 Mar 2018
    

    当然,我确实必须重新安装 python 包。

    来源:https://github.com/ianunruh/hvac/issues/75

    【讨论】:

      【解决方案6】:

      我认为 python 已经认识到这是一个问题:https://www.python.org/downloads/release/python-2715/

      注意

      macOS 用户注意:从 2.7.15 开始,所有 python.org macOS 安装程序 附带 OpenSSL 的内置副本。此外,还有一个新的 macOS 10.9+ 的附加安装程序变体,包括内置 Tcl/Tk 8.6 版本。有关详细信息,请参阅安装程序自述文件。

      只需安装 2.7.15 即可解决我的 OpenSSL 问题。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-10-30
        • 1970-01-01
        • 2020-05-29
        • 1970-01-01
        • 2013-09-09
        • 1970-01-01
        相关资源
        最近更新 更多