import socket import traceback import time sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.setblocking(0) # use a non-exist host/port, or a high delay remote port here as target r = sock.connect_ex(('1.2.3.4', 12345)) print r try: sock.send('abc') except socket.error: traceback.print_exc(10) print 'in asyncore main loop, this exception is caught and handle_error() is called' # for a non-exist target port, use long sleep time(1000 for example) to see the tcp syn retransfer in tcpdump/wireshark # for a high delay target port, shorter sleep may be used(say, 10 seconds), and the second .send call() will be successful time.sleep(10) sock.send('abc') time.sleep(15) sock.close()