ssl.SSLError:[SSL:CERTIFICATE_VERIFY_FAILED]证书验证Python失败

时间:2017-06-04 16:58:05

标签: python ssl

我想使用SSL连接到IRC。我写在Python 2.7。但是,对于以下代码:

HOST = 'chat.freenode.net'
PORT = 7000
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
code = sock.connect_ex((HOST, PORT))
context = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2)
context.verify_mode = ssl.CERT_REQUIRED
context.load_verify_locations('COMODOECCCertificationAuthority.crt')
secure_sock = context.wrap_socket(sock)

PyCharm在行secure_sock = context.wrap_socket(sock)中显示错误有什么问题?

1 个答案:

答案 0 :(得分:1)

  

context.load_verify_locations( 'COMODOECCCertificationAuthority.crt')

我不知道文件COMODOECCCertificationAuthority.crt中的内容,但看起来您期望通过Comodo签署的证书。但是,chat.freenode.net证书的发行者是Let's Encrypt而不是Comodo。您可以使用

获取链
$ openssl s_client -connect chat.freenode.net:7000
...
Certificate chain
0 s:/CN=cherryh.freenode.net
  i:/C=US/O=Let's Encrypt/CN=Let's Encrypt Authority X3
1 s:/C=US/O=Let's Encrypt/CN=Let's Encrypt Authority X3
  i:/O=Digital Signature Trust Co./CN=DST Root CA X3

这意味着您需要信任的根CA是“DST Root CA X3”。您可以下载此CA here的证书。如果您在load_verify_locations中将其添加为受信任,则可以使用。

相关问题