Verifying HTTPS certificates with urllib.request

后端 未结 6 711
不知归路
不知归路 2021-01-18 00:07

I am trying to open an https URL using the urlopen method in Python 3\'s urllib.request module. It seems to work fine, but the documentation warns that \"[i]f neither

6条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-18 00:28

    Elias Zamarias answer still works, but gives a deprecation warning:

    DeprecationWarning: cafile, cpath and cadefault are deprecated, use a custom context instead.
    

    I was able to solve the same problem this way instead (using Python 3.7.0):

    import ssl
    import urllib.request
    
    ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
    response = urllib.request.urlopen("http://www.example.com", context=ssl_context)
    

提交回复
热议问题