import imaplib, poplib, smtplib, ssl, threading, time, random ctx = ssl.create_default_context() ctx.check_hostname = False ctx.verify_mode = ssl.CERT_NONE USER = "barant@barant.com" PASS = "imaptest123" errors = [] lock = threading.Lock() def note_error(name, e): with lock: errors.append(f"{name}: {e}") def imap_cycle(i): try: m = imaplib.IMAP4_SSL("127.0.0.1", 993, ssl_context=ctx) m.login(USER, PASS) m.select("INBOX") m.search(None, "ALL") typ, data = m.fetch("1", "(FLAGS UID)") m.logout() except Exception as e: note_error(f"imap#{i}", e) def pop3_cycle(i): try: m = poplib.POP3_SSL("127.0.0.1", 995, context=ctx) m.user(USER) m.pass_(PASS) m.stat() m.list() m.quit() except Exception as e: note_error(f"pop3#{i}", e) def smtp_cycle(i): try: s = smtplib.SMTP("127.0.0.1", 25, timeout=10) s.ehlo("stress-client") msg = f"From: stress{i}@barant.com\r\nTo: barant@barant.com\r\nSubject: stress {i}\r\n\r\nstress body {i}\r\n" s.sendmail(f"stress{i}@barant.com", ["barant@barant.com"], msg) s.quit() except Exception as e: note_error(f"smtp#{i}", e) def rude_disconnect(i): # Connect and drop mid-handshake / mid-command without clean close - # exercises the abrupt-disconnect path specifically. import socket try: s = socket.create_connection(("127.0.0.1", 993), timeout=5) s.recv(50) s.close() except Exception as e: note_error(f"rude#{i}", e) threads = [] N = 40 for i in range(N): fn = random.choice([imap_cycle, pop3_cycle, smtp_cycle, rude_disconnect]) t = threading.Thread(target=fn, args=(i,)) threads.append(t) t.start() time.sleep(0.02) for t in threads: t.join(timeout=15) print(f"Completed {N} concurrent mixed-protocol cycles.") print(f"Errors: {len(errors)}") for e in errors[:20]: print(" ", e)