from socket import socket, SOL_SOCKET, SO_REUSEADDR import ssl with socket() as server: server.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1) server.bind(("localhost", 119)) server.listen() [client, _] = server.accept() with client: client.sendall(b"200 Server ready\r\n") cmd = client.recv(3000) assert cmd == b"CAPABILITIES\r\n" client.sendall( b"101 Capability list:\r\n" b"VERSION 2\r\n" b"STARTTLS\r\n" b".\r\n" ) cmd = client.recv(3000) assert cmd == b"STARTTLS\r\n" client.sendall(b"382 Begin TLS negotiation now\r\n") certfile = "Lib/test/keycert3.pem" tls = ssl.wrap_socket(client, server_side=True, certfile=certfile) with tls: cmd = tls.recv(3000) assert cmd == b"CAPABILITIES\r\n" tls.sendall( b"101 Capability list:\r\n" b"VERSION 2\r\n" b".\r\n" ) cmd = tls.recv(3000) assert cmd == b"QUIT\r\n" tls.sendall(b"205 Bye!\r\n")