python培训之ssl报错怎么解决

发布时间:2022-10-18 09:47:50 人气:124 作者:多测师

  Python2.7.9 之后,当使用urllib.urlopen打开一个 https 链接时,会验证一次 SSL 证书。而当目标网站使用的是自签名的证书时就会抛出如下异常:

  urlopen error (SSL: CERTIFICATE_VERIFY_FAILED) certificate verify failed (_ssl.c:777)

  解决办法如下

  1,方案一

  使用ssl创建未经验证的上下文,在urlopen中传入上下文参数:

python培训之ssl报错怎么解决

  import ssl

  context = ssl._create_unverified_context()

  urllib.request.urlopen(req,context=context)

  2,方案二

  全局取消证书验证:

  import ssl

  ssl._create_default_https_context = ssl._create_unverified_context

  urllib2.urlopen("https://www.12306.cn/mormhweb/").read()

  3,方案三

  使用的是requests模块,将方法中的verify设置位False即可:

  requests.get(url, headers=Hostreferer,verify=False)

  以上内容为大家介绍了python培训之ssl报错怎么解决,希望对大家有所帮助,如果想要了解更多Python相关知识,请关注多测师。https://www.e70w.com/xwzx/


返回列表
在线客服
联系方式

热线电话

17727591462

上班时间

周一到周五

二维码
线