解决Python requests 报错方法集锦

yipeiwu_com6年前Python基础

python版本和ssl版本都会导致 requests在请求https网站时候会出一些错误,最好使用新版本。

1 Python2.6x use requests

一台老Centos机器上跑着古老的应用,加了一个新模块之后报错 报错 InsecurePlatformWarning: A true SSLContext object is not available.

/usr/lib/python2.6/site-packages/requests/packages/urllib3/util/ssl_.py:132: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. You can upgrade to a newer version of Python to solve this. For more information, see https://urllib3.readthedocs.io/en/latest/advanced-usage.html
  InsecurePlatformWarning

解决办法

使用老版本的 requests

$pip install requests==2.5.3

或者这样安装

$ pip install requests[security]

2 SSL3_GET_SERVER_CERTIFICATE:certificate verify failed

错误如下 版本 python2.7.5

Traceback (most recent call last):
  File "./test.py", line 24, in <module>
  response = requests.get(url1, headers=headers)
  File "build/bdist.linux-x86_64/egg/requests/api.py", line 52, in get
  File "build/bdist.linux-x86_64/egg/requests/api.py", line 40, in request
  File "build/bdist.linux-x86_64/egg/requests/sessions.py", line 209, in request
  File "build/bdist.linux-x86_64/egg/requests/models.py", line 624, in send
  File "build/bdist.linux-x86_64/egg/requests/models.py", line 300, in _build_response
  File "build/bdist.linux-x86_64/egg/requests/models.py", line 611, in send
requests.exceptions.SSLError: [Errno 1] _ssl.c:503: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed

解决方法

可以禁用 verify

>>> requests.get('https://google.com', verify=True)
问题地址 python-requests-throwing-up-sslerror

3 SSLError: bad handshake

SSLError: bad handshake: Error([(‘SSL routines', ‘SSL3_GET_SERVER_CERTIFICATE', ‘certificate verify failed')],) python2.7.5
pip uninstall -y certifi && pip install certifi==2015.04.28

讨论参见: https://github.com/rackspace/pyrax/issues/601

以上所述是小编给大家介绍的Python requests 报错方法集锦,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!

相关文章

Python中的数据对象持久化存储模块pickle的使用示例

Python中可以使用 pickle 模块将对象转化为文件保存在磁盘上,在需要的时候再读取并还原。具体用法如下: pickle是Python库中常用的序列化工具,可以将内存对象以文本或二...

Python的IDEL增加清屏功能实例

为idle增加一个清屏的扩展ClearWindow就可以了(在http://bugs.python.org/issue6143中可以看到这个扩展的说明)。 下面我说安装使用的方法。首先下...

使用Anaconda3建立虚拟独立的python2.7环境方法

由于一些不可预测的因素,必须使用python2.7进行开发,所以研究了一下怎么在Anaconda3下建立2.7的开发环境,发现十分方便,在此分享一下。 首先安装Anaconda3,这就不...

python中import reload __import__的区别详解

import 作用:导入/引入一个python标准模块,其中包括.py文件、带有__init__.py文件的目录(自定义模块)。 import module_name[,module...

Python 解析XML文件

Python文件: 复制代码 代码如下:#parsexml.py #本例子参考自python联机文档,做了适当改动和添加 import xml.parsers.expat #控制打印缩进...