最近,我在Python 2.7.6(Ubuntu 14.04.2 LTS)环境中将requests库的版本从2.5.3升级到2.6.0,却遇到了’A true SSLContext object is not available’警告。每当我在Python 2.7.6环境中尝试使用requests库访问’github’时,都会看到这个警告。
mkvirtualenv requests260 -i requests==2.6.0
....
(requests260)dave@xxps:~$ python
Python 2.7.6 (default, Mar 22 2014, 22:59:56)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import requests
>>> resp = requests.get('https://github.com')
/home/dave/.virtualenvs/requests260/local/lib/python2.7/site-packages/requests/packages/urllib3/util/ssl_.py:79: 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
Lukasa commented at 2015-03-15 11:46:
That warning should not be raised unless we're not using pyOpenSSL、Can you confirm that `pip freeze` lists pyOpenSSL, ndg-httpsclient, and pyasn1?
davecoutts commented at 2015-03-15 16:14:
Oooops! I had convinced myself that pyOpenSSL was installed, but it wasn解决方案
通过研究,我找到了解决这个问题的方法。如果您在Python 2.7.6环境中使用requests 2.6.0,可以通过添加ndg-httpsclient模块来避免警告,并降低pyOpenSSL导入时间的影响。
**解决方案:**
以下是解决问题的步骤:
1、创建一个虚拟环境(如果您尚未创建),并激活它:
```bash
mkvirtualenv requests260_ndghttpsclient
workon requests260_ndghttpsclient
2、使用pip安装requests 2.6.0和ndg-httpsclient:
pip install requests==2.6.0 ndg-httpsclient
3、使用pip freeze检查已安装的模块,确保ndg-httpsclient已被添加:
pip freeze
您应该看到类似以下的输出:
cffi==0.9.2
cryptography==0.8
enum34==1.0.4
ndg-httpsclient==0.3.3
pyasn1==0.1.7
pycparser==2.10
pyOpenSSL==0.14
requests==2.6.0
six==1.9.0
4、现在,您可以在Python 2.7.6环境中使用requests 2.6.0,而不再看到’A true SSLContext object is not available’警告。
这个解决方案的关键是安装ndg-httpsclient模块,它有助于避免警告并改善pyOpenSSL导入性能。虽然在某些情况下,添加pyOpenSSL可能会导致导入时间增加,但通过添加ndg-httpsclient,您可以同时解决警告问题并减少导入时间。
希望这篇文章对您解决在Python 2.7.6环境中使用requests 2.6.0时遇到的问题有所帮助。如果您有任何疑问或需要进一步的协助,请随时提出。