How to allow python to trust my server's TLS self-signed certificate: ssl.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed

前端 未结 1 550
鱼传尺愫
鱼传尺愫 2021-01-22 02:45

This is not a duplicate for this post. I tried the solutions there and nothing works in my case.

I am using Windows and Python 3.6.5. I have a python script for a TLS c

相关标签:
1条回答
  • This question has been idle for a while, but in case somebody is still struggling with connecting to a server with a self-signed certificate via Python ssl library:

    You can use the load_verify_locations method of SSLContext to specify custom self-signed root certs (see Python Docs for load_verify_locations).

    The forementioned code could be extended as follows:

    ...
    
    context = ssl.SSLContext() 
    context.verify_mode = ssl.CERT_OPTIONAL 
    context.check_hostname = False
    
    context.load_verify_locations(cafile='/path/to/your/cacert.pem')
    
    ...
    
    

    Be aware that you also need to include public root certs in case you want to connect to other servers with public certificates with the same client/context. (you could for example append your cacert.pem content to the certifi root certificates and reference that folder / path instead).

    See also this Python docs paragraph for more information: client-side operations.

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