如何在 python 3.x 中禁用 ssl 检查? [英] How do I disable the ssl check in python 3.x?
问题描述
我正在使用 urllib.request.urlretrieve 将文件下载到本地.
I'm using urllib.request.urlretrieve to download a file to local.
urllib.request.urlretrieve(url_string,file_name)
它抛出错误:
ssl.CertificateError 未被用户代码处理消息:主机名foo.net"与a248.e.akamai.net"、.akamaihd.net"、.akamaihd-staging.net"、.akamaized.net', '.akamaized-staging.net'
ssl.CertificateError was unhandled by user code Message: hostname 'foo.net' doesn't match either of 'a248.e.akamai.net', '.akamaihd.net', '.akamaihd-staging.net', '.akamaized.net', '.akamaized-staging.net'
如果您将网址复制到 Chrome 中,它会向您显示一条通知,您需要说继续访问该网址"之类的内容.
If you copy the url into Chrome, it will show you a notification and you need to say something like "keep going to the url".
推荐答案
使用 urllib.request.urlopen
与 自定义 ssl 上下文:
import ssl
import urllib.request
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
with urllib.request.urlopen(url_string, context=ctx) as u, \
open(file_name, 'wb') as f:
f.write(u.read())
或者,如果您使用 requests
库,它可以更简单:
Alternatively, if you use requests
library, it could be simpler:
import requests
with open(file_name, 'wb') as f:
resp = requests.get(url_string, verify=False)
f.write(resp.content)
这篇关于如何在 python 3.x 中禁用 ssl 检查?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!