python用requests请求百度接口报“SSL: CERTIFICATE_VERIFY_FAILED”
2015-11-18 16:58
776 查看
SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:581)
今天想试用一下百度的语音识别API,附带步骤:
1. 先去百度开放云平台注册,成为开发者,审核可能需要时间的,我去年申过现在账号还在
2. 然后创建一个应用
3.为创建完的应用添加服务,有俩,语音识别和语音生成
4. 这样我就有一个调用他语音识别接口的access_token了,这个token由于我采用的是API
For Rest,要拿API_key和secret_key通过一个http请求获得,问题就出在这儿了
我用request按照他文档的样子Post了一下,又Get了一下都报一个验证失败的错误。
他告诉我:
SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:581)
找了一下,有人说原因是这样的:
Python 2.7.9 之后引入了一个新特性
当你urllib.urlopen一个 https 的时候会验证一次 SSL 证书
当目标使用的是自签名的证书时就会爆出一个
urllib2.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:581)> 的错误消息
确实我用urllib试了一下结果一样,requests跟urllib是一样的。
那么要解决这个问题,PEP-0476的文档说
For users who wish to opt out of certificate verification on a single connection, they can achieve
this by providing the contextargument
to urllib.urlopen :
is also possible, though
highly discouraged , to globally disable verification by monkeypatching the ssl module
in versions of Python that implement this PEP:
就是说你可以禁掉这个证书的要求,urllib来说有两种方式,一种是urllib.urlopen()有一个参数context,把他设成ssl._create_unverified_context
或者修改现在的全局默认值
或
为
ssl._create_unverified_context
测试了一下,确实可以,返回了几个token,那么requests呢,难道必须设置全局变量吗。
其实request的post和get都有一个叫verify的参数,把他设成False就可以了。
今天想试用一下百度的语音识别API,附带步骤:
1. 先去百度开放云平台注册,成为开发者,审核可能需要时间的,我去年申过现在账号还在
2. 然后创建一个应用
3.为创建完的应用添加服务,有俩,语音识别和语音生成
4. 这样我就有一个调用他语音识别接口的access_token了,这个token由于我采用的是API
For Rest,要拿API_key和secret_key通过一个http请求获得,问题就出在这儿了
我用request按照他文档的样子Post了一下,又Get了一下都报一个验证失败的错误。
requests.post('https://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials&client_id=xxxxxxx&client_secret=xxxxxxx').content
requests.get('https://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials&client_id=xxxxxxx&client_secret=xxxxxxx').content
他告诉我:
SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:581)
找了一下,有人说原因是这样的:
Python 2.7.9 之后引入了一个新特性
当你urllib.urlopen一个 https 的时候会验证一次 SSL 证书
当目标使用的是自签名的证书时就会爆出一个
urllib2.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:581)> 的错误消息
确实我用urllib试了一下结果一样,requests跟urllib是一样的。
那么要解决这个问题,PEP-0476的文档说
For users who wish to opt out of certificate verification on a single connection, they can achieve
this by providing the contextargument
to urllib.urlopen :
import ssl # This restores the same behavior as before. context = ssl._create_unverified_context() urllib.urlopen("https://no-valid-cert", context=context)It
is also possible, though
highly discouraged , to globally disable verification by monkeypatching the ssl module
in versions of Python that implement this PEP:
import ssl try: _create_unverified_https_context = ssl._create_unverified_context except AttributeError: # Legacy Python that doesn't verify HTTPS certificates by default pass else: # Handle target environment that doesn't support HTTPS verification ssl._create_default_https_context = _create_unverified_https_context
就是说你可以禁掉这个证书的要求,urllib来说有两种方式,一种是urllib.urlopen()有一个参数context,把他设成ssl._create_unverified_context
或者修改现在的全局默认值
_create_unverified_https_context
或
ssl._create_default_https_context
为
ssl._create_unverified_context
测试了一下,确实可以,返回了几个token,那么requests呢,难道必须设置全局变量吗。
其实request的post和get都有一个叫verify的参数,把他设成False就可以了。
print requests.get('https://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials&client_id=xxxxx&client_secret=xxxxxxxx', verify=False).content
相关文章推荐
- Python学习指南
- python3输出unicode
- Python中list的实现
- Python处理Json报文
- 【python】class之子类
- python 抽象类、抽象方法的实现
- 安装python MySQLdb报错:pymemcompat.h:10:20: 致命错误的解决
- scikit学习心得——Isotonic Regression
- Python编写微信打飞机小游戏(十二)
- Difference between Range and Xrange in Python
- 折腾Ipython
- 第一个Python程序——博客自动访问脚本
- Python基础学习-爬虫小试2
- python插件
- Python学习小记
- 让cherrypy监听系统内所有活动网卡传入的请求
- python 远程操作ubuntu
- Python 创建函数和代码重用
- python数据类型详解
- Python基础练习