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

前端 未结 2 1052
粉色の甜心
粉色の甜心 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:46

    Function urllib.request.urlretrieve doesn't accept any SSL options but urllib.request.urlopen does.

    However instead creating a secure SSL context with ssl.create_default_context() and making it insecure you can create an insecure context with ssl.SSLContext():

    This:

    ctx = ssl.create_default_context()
    ctx.check_hostname = False
    ctx.verify_mode = ssl.CERT_NONE
    

    is equivalent to:

    ctx = ssl.SSLContext()
    

    (For Python < 3.5.3 use ssl.SSLContext(ssl.PROTOCOL_TLSv1))

    Which makes a nice one-liner:

    import ssl
    import urllib.request
    
    with urllib.request.urlopen("https://wrong.host.badssl.com/", context=ssl.SSLContext()) as url:
        print(url.read())
    

提交回复
热议问题