Skip to content

Instantly share code, notes, and snippets.

@tcpdump-examples
Created January 18, 2022 13:22
Show Gist options
  • Save tcpdump-examples/fddc3f1c1a3b5940637f266d0d343646 to your computer and use it in GitHub Desktop.
Save tcpdump-examples/fddc3f1c1a3b5940637f266d0d343646 to your computer and use it in GitHub Desktop.

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

<urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:777)>

二、解决方案 1,方案一 使用ssl创建未经验证的上下文,在urlopen中传入上下文参数:

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)

how to fix CERTIFICATE_VERIFY_FAILED

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment