The Asyncore Module
This is an excerpt from the Python Standard Library.
The asyncore module provides a “reactive” socket implementation. Instead of creating socket objects, and calling methods on them to do things, this module lets you write code that is called when something can be done. To implement an asynchronous socket handler, subclass the dispatcher class, and override one or more of the following methods:
-
writable is called by the asyncore framework to check if the dispatcher has data to send. The default implementation always returns true.
-
readable is called to check if the dispatcher is ready to process incoming data. The default implementation always returns true. Note that this doesn’t mean that there is data available; the framework will call handle_read when data is ready.
-
handle_connect is called when a connection is successfully established.
-
handle_expt is called when a connection fails (Windows) and/or out-of-band data arrives.
-
handle_accept is called when a connection request is made to a listening socket. The callback should call the accept method to get the client socket.
-
handle_read is called when there is data waiting to be read from the socket. The callback should call the recv method to get the data.
-
handle_write is called when data can be written to the socket. Use the send method to write data.
-
handle_close is called when the socket is closed or reset.
-
handle_error(type, value, traceback) is called if a Python error occurs in any of the other callbacks. The default implementation prints an abbreviated traceback to sys.stdout.