Python 2.7.11出现AttributeError: 'module' object has no attribute 'PROTOCOL_TLSv1_2'错误。

11

我在我的Mac上安装Python时遇到了无法获取TLS v1.2支持的问题。

这是我的OpenSSL版本:

openssl version
OpenSSL 1.0.2h  3 May 2016

这是我的Python版本:

python --version
Python 2.7.11

这是我的测试方式:

>>> import ssl
>>> ssl.PROTOCOL_TLSv1_2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'PROTOCOL_TLSv1_2'

阅读此内容:https://docs.python.org/2/library/ssl.html 它说:

由于调用操作系统套接字 API,某些行为可能因平台而异。安装的 OpenSSL 版本也可能导致行为差异。例如,TLSv1.1 和 TLSv1.2 随 openssl 版本 1.0.1 一起提供。

所以我需要使用OpenSSL 1.0.2h来支持tlsv1.2。

有人有什么想法吗?


2
请检查您是否安装了新版本的openssl,并确保在Python中实际使用它,即import ssl\nprint ssl.OPENSSL_VERSION - Steffen Ullrich
1
是的,那就是问题所在。我重新使用“brew install python --with-brewed-openssl”进行了安装。 - gprime
gprime,你能把它写成答案吗? - Leandro David
1个回答

4
问题是Python使用的openssl版本不支持TLS 1.2。我使用brew安装了openssl,然后重新安装Python并告诉它使用我用brew安装的openssl。
$ brew update
$ brew install openssl
$ brew install python --with-brewed-openssl

然后一切都正常运行!


1
有没有不需要安装最新版Python的解决方案? - Vitaly Zdanevich
抱歉,据我所知没有。 - gprime
2
@gprime,Ubuntu有类似的解决方案吗?我遇到了完全相同的问题,但是在Ubuntu上使用Django。 - ifconfig
3
今天我尝试了这个:错误:无效选项:--with-brewed-openssl - slashdottir
无论如何,现在Homebrew基本上“已经使用了一个酿造的openssl”(尽管有一个特定的版本,请执行brew edit python以查看)。 - rogerdpack

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接