Python Frequently Asked Questions, Release 3.9.5 57 4.5. Network/Internet Programming Note: The $_{asyncio}$module provides a general purpose single-threaded and concurrent asynchronous library, You can use the$_{socket.connect_ex()}$ method to avoid creating an exception. It will just return the errno value. To poll, you can call$_{socket.connect_ex()}$ again later –$_{0}$or$_{errno.EISCONN}$ indicate that you’re connected – or you can pass this socket to$_{select.select()}$ to check if it’s writable. To prevent the TCP connect from blocking, you can set the socket to non-blocking mode. Then when you do the socket.connect() , you will either connect immediately (unlikely) or get an exception that contains the error number as$_{.errno}$ . errno.EINPROGRESS indicates that the connection is in progress, but hasn’t finished yet. Different OSes will return different values, so you’re going to have to check what’s returned on your system. The$_{select}$module is commonly used to help with asynchronous I/O on sockets. A Unix-only alternative uses sendmail. The location of the sendmail program varies between systems; sometimes it is$_{/usr/lib/sendmail}$ , sometimes$_{/usr/sbin/sendmail}$ . The sendmail manual page will help you out. Here’s some sample code: Here’s a very simple interactive mail sender that uses it. This method will work on any host that supports an SMTP listener. Use the standard library module$_{smtplib}$ . You can find a collection of useful links on the Web Programming wiki page. 4.5.3 What module should I use to help with generating HTML? 4.5.4 How do I send mail from a Python script? import sys , smtplib fromaddr =input("From: ") toaddrs =input("To: ").split( ',') print("Enter message, end with ^D: ") msg ='' while True : line =sys.stdin.readline() if not line: break msg +=line # The actual mail send server =smtplib.SMTP('localhost') server.sendmail(fromaddr, toaddrs, msg) server.quit() import os SENDMAIL ="/usr/sbin/sendmail " # sendmail location p =os.popen( " %s -t -i "%SENDMAIL, "w") p.write("To: receiver@example.com ") p.write("Subject: test ") p.write(" ") # blank line separating headers from body p.write("Some text ") p.write("some more text ") sts =p.close() if sts !=0: print("Sendmail exit status", sts) 4.5.5 How do I avoid blocking in the connect() method of a socket?