AttributeError: '_socketobject' object has no attribute 'set_tlsext_host_name'

AttributeError: '_socketobject' object has no attribute 'set_tlsext_host_name'

在 python 中,在 Ubuntu 服务器上,我试图让 requests 库发出 https 请求,如下所示:

import requests
requests.post("https://example.com")

起初,我得到以下信息:

/usr/local/lib/python2.7/dist-packages/requests/packages/urllib3/util/ssl_.py:90: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning.

在遵循这个问题的建议后:InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately,我现在将我的警告升级为以下错误:

AttributeError: '_socketobject' object has no attribute 'set_tlsext_host_name'

我该如何解决这个问题?

我的修复如下:

sudo apt-get purge python-openssl
sudo pip install pyopenssl

这能够让 python 为我在 Ubuntu 12.04.3 LTS 上工作。

sudo apt-get install python-dev python-pip build-essential libffi-dev
sudo -H pip install --upgrade pip setuptools && sudo -H pip install --upgrade pyopenssl

在 RedHat 上:

sudo yum remove pyOpenSSL
sudo pip install pyopenssl