import socket, sys, threading def handle(conn): conn.sendall(b"220 sink ready\r\n") buf = b"" while True: data = conn.recv(65536) if not data: break buf += data if b"QUIT" in data: break open(r"C:\Code\Plushealthtech\tmp-mailtest\raw_capture.bin", "wb").write(buf) try: conn.sendall(b"221 bye\r\n") except Exception: pass conn.close() s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) s.bind(("127.0.0.1", 2525)) s.listen(1) print("listening on 127.0.0.1:2525", flush=True) c, _ = s.accept() handle(c) print("done", flush=True)