import socket import ssl ctx = ssl.create_default_context() ctx.check_hostname = False ctx.verify_mode = ssl.CERT_NONE raw = socket.create_connection(("127.0.0.1", 993), timeout=10) s = ctx.wrap_socket(raw) def recv_line(): buf = b"" while not buf.endswith(b"\r\n"): buf += s.recv(1) print("S:", buf) return buf recv_line() def send(line): print("C:", line) s.sendall(line.encode() + b"\r\n") send('a1 LOGIN "barant@barant.com" "imaptest123"') recv_line()