AttributeError:“模块”对象在Python 2.7.11中没有属性“ PROTOCOL_TLSv1_2”


问题内容

我在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版本也可能导致行为变化。例如,TL​​Sv1.1和TLSv1.2随附openssl版本1.0.1。

因此,我在安装OpenSSL 1.0.2h时对tlsv1.2支持有要求。

谁有想法?


问题答案:

问题是python使用的openssl版本不支持TLS 1.2。

我用brew安装了openssl,然后重新安装了python,告诉它使用我通过brew安装的openssl:

$ brew update
$ brew install openssl
$ brew install python --with-brewed-openssl

然后一切正常!