urllib2报错[SSL:CERTIFICATE_VERIFY_FAILED]

在python中使用urllib2库去访问一个自签名的网站时,会出现如下报错: urllib2.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:590)>

出现以上错误的原因是因为python的版本问题,在python2.6(含2.6)以下版本中,在访问HTTPS的网站时,TLS握手期间不会检查服务器X509的证书签名是否是CA的可信任根证书。这种局面在python2.7 3.4 和 3.5版本中得到了修改。

所以,以下代码在python2.6版本中测试是完全没有问题的


import json
import urllib
import urllib2

url='https://www.20150509.cn:1559'

def test():
  
  pre_data = [{"client":"local", "tgt":"*", "fun":"test.ping"}]

  json_data = json.dumps(pre_data)
  
  header = {"Content-Type":"application/json", "Accept":"application/json", "X-Auth-Token":"b91e7uj86g4f97cc**********b92778ujh4kedf"}

  request = urllib2.Request(url, json_data, header)

  response = urllib2.urlopen(request)

  html = response.read()

  print html


if __name__=="__main__":
  test()

运行测试

shell> python sa.py 
{"return": [{"vm3.salt.com": true, "vm2.salt.com": true, "ph1.salt.com": true, "ph2.salt.com": true, "vm1.salt.com": true, "vm4.salt.com": true, "localhost": true, "vm7.salt.com": true}]}


在python2.7+版本就会报上述错误

shell> python sa.py 
Traceback (most recent call last):
  File "sa.py", line 25, in <module>
    test()
  File "sa.py", line 17, in test
    response = urllib2.urlopen(request)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 154, in urlopen
    return opener.open(url, data, timeout)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 431, in open
    response = self._open(req, data)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 449, in _open
    '_open', req)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 409, in _call_chain
    result = func(*args)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 1240, in https_open
    context=self._context)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 1197, in do_open
    raise URLError(err)
urllib2.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:590)>

解决方法:

import json
import urllib
import urllib2
import ssl   #add line 1

ssl._create_default_https_context = ssl._create_unverified_context  #add line 2

url='https://www.20150509.cn:1559'

def test():
  
  pre_data = [{"client":"local", "tgt":"*", "fun":"test.ping"}]

  json_data = json.dumps(pre_data)
  
  header = {"Content-Type":"application/json", "Accept":"application/json", "X-Auth-Token":"b91e7uj86g4f97cc**********b92778ujh4kedf"}

  request = urllib2.Request(url, json_data, header)

  response = urllib2.urlopen(request)

  html = response.read()

  print html


if __name__=="__main__":
  test()


参考文档:

http://https://www.python.org/dev/peps/pep-0476/

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • # Python 资源大全中文版 我想很多程序员应该记得 GitHub 上有一个 Awesome - XXX 系列...
    aimaile阅读 26,634评论 6 427
  • 前言 Python的创始人为Guido van Rossum。1989年圣诞节期间,在阿姆斯特丹,Guido为了打...
    依依玖玥阅读 3,622评论 6 37
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 174,384评论 25 709
  • 说什么我也不会将就的!一生一世一双人,相思相望不相亲。 都说在校园里的爱情走不到天荒地老,但我信,校园里的爱情还是...
    小花孟阅读 189评论 0 1
  • 同学们,老师们。大家好。 大名人高尔基曾经说过:书籍是人类进步的阶梯,人类的进步,都是由看书才可以进步的,最近有一...
    百合花中哥哥阅读 1,630评论 0 0