How do I disable the ssl check in python 3.x?

前端 未结 2 1037
粉色の甜心
粉色の甜心 2020-12-03 14:00

I\'m using urllib.request.urlretrieve to download a file to local.

urllib.request.urlretrieve(url_string,file_name)

It throws error:

<
2条回答
  •  借酒劲吻你
    2020-12-03 14:30

    Use urllib.request.urlopen with custom ssl context:

    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())
    

    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)
    

提交回复
热议问题