Verifying HTTPS certificates with urllib.request

后端 未结 6 745
不知归路
不知归路 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:12

    I found a library that does what I'm trying to do: Certifi. It can be installed by running pip install certifi from the command line.

    Making requests and verifying them is now easy:

    import certifi
    import urllib.request
    
    urllib.request.urlopen("https://example.com/", cafile=certifi.where())
    

    As I expected, this returns a HTTPResponse object for a site with a valid certificate and raises a ssl.CertificateError exception for a site with an invalid certificate.

提交回复
热议问题