Wireshark 未检测到 TLS 连接

Wireshark not detecting TLS connection

我想做TLS通讯,但是用WireShark查的时候说是普通的TCP连接,没有使用TLS。不应该,因为有证书。

客户:

import socket
import ssl

hostname = 'localhost'
# PROTOCOL_TLS_CLIENT requires valid cert chain and hostname
context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
context.load_verify_locations('certificates/ca.pem')

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

ssock = context.wrap_socket(sock, server_hostname=hostname)
ssock.connect((hostname, 8080))

print(ssock.version())
ssock.send("TLSnotWorking".encode("UTF-8"))

ssock.close()

服务器:

import ssl
import socket

context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
context.load_cert_chain('certificates/server.pem', 'certificates/server.key')

with socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0) as sock:
    sock.bind(('127.0.0.1', 8080))
    sock.listen(5)
    with context.wrap_socket(sock, server_side=True) as ssock:
        try:
            while True:
                conn, addr = ssock.accept()
                
                try:
                    while True:
                        msg = conn.recv(4096)
                        if not len(msg):
                            conn.close()
                            break

                        print(msg.decode("UTF-8"))

                except Exception as e:
                    print(e)
        except KeyboardInterrupt:
                conn.close()
                ssock.shutdown(socket.SHUT_RDWR)
                ssock.close()

wireshark 可以判断是否是 TLS,但我只是使用了错误的端口,例如“8443”端口,您可以查看是否有 TLS 通信