首页 > 解决方案 > Python SSL 套接字错误 ssl.SSLCertVerificationError

问题描述

我正在测试 Python SSL 文档中的示例,
然后我尝试使用我自己的证书,它会弹出这个:

ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1129)

我知道这个错误正在发生,因为我使用的是自签名证书,
但我不会花很多钱,因为它需要改变。
我想跳过验证,所以它将使用证书作为加密。
我怎样才能使用该代码做到这一点:

客户:

import socket
import ssl
hostname = 'localhost'
context = ssl.create_default_context()

with socket.create_connection((hostname, 443)) as sock:
    with context.wrap_socket(sock, server_hostname=hostname) as ssock:
        print(ssock.version())

服务器:

context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
context.load_cert_chain('certs/setver.crt', 'certs/private.key')
with socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0) as sock:
    sock.bind(('127.0.0.1', 8443))
    sock.listen(5)
    with context.wrap_socket(sock, server_side=True) as ssock:
        conn, addr = ssock.accept()
        # Rest of the code...

标签: pythonpython-3.xsocketsssl

解决方案


推荐阅读