Python 3 urllib ignore SSL certificate verification

前端 未结 2 1509
一生所求
一生所求 2021-02-05 05:50

I have a server setup for testing, with a self-signed certificate, and want to be able to test towards it.

How do you ignore SSL verification in the Python 3 ver

相关标签:
2条回答
  • 2021-02-05 06:11

    The accepted answer just gave advise to use python 3.5+, instead of direct answer. It causes confusion.

    For someone looking for a direct answer, here it is:

    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 f:
        f.read(300)
    

    Alternatively, if you use requests library, it has much better API:

    import requests
    
    with open(file_name, 'wb') as f:
        resp = requests.get(url_string, verify=False)
        f.write(resp.content)
    

    The answer is copied from this post (thanks @falsetru): How do I disable the ssl check in python 3.x?

    These two questions should be merged.

    0 讨论(0)
  • 2021-02-05 06:18

    Python 3.0 to 3.3 does not have context parameter, It was added in Python 3.4. So, you can update your Python version to 3.5 to use context.

    0 讨论(0)
提交回复
热议问题