前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python中用requests时遇到的错误警告解决方案

Python中用requests时遇到的错误警告解决方案

原创
作者头像
华科云商小徐
发布2023-11-20 11:22:54
2330
发布2023-11-20 11:22:54
举报
文章被收录于专栏:小徐学爬虫小徐学爬虫

最近,我在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’时,都会看到这个警告。

代码语言:javascript
复制
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:

代码语言:javascript
复制
pip install requests==2.6.0 ndg-httpsclient

3、使用pip freeze检查已安装的模块,确保ndg-httpsclient已被添加:

代码语言:javascript
复制
pip freeze

您应该看到类似以下的输出:

代码语言:javascript
复制
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时遇到的问题有所帮助。如果您有任何疑问或需要进一步的协助,请随时提出。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档