Verifying HTTPS certificates with urllib.request

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

    Different Linux distributives have different pack names. I tested in Centos and Ubuntu. These certificate bundles are updates with system update. So you may just detect which bundle is available and use it with urlopen.

    cafile = None
    for i in [
        '/etc/ssl/certs/ca-bundle.crt',
        '/etc/ssl/certs/ca-certificates.crt',
    ]:
        if os.path.exists(i):
            cafile = i
            break
    if cafile is None:
        raise RuntimeError('System CA-certificates bundle not found')
    

提交回复
热议问题