import asyncore import socket import sys class listener(asyncore.dispatcher): def __init__( self, address): asyncore.dispatcher.__init__( self) self.create_socket( socket.AF_INET, socket.SOCK_STREAM) self.set_reuse_addr() self.bind( address) self.listen( 5) def handle_accept( self): sock, junk = self.accept() chatter(sock=sock) def writable( self): return False class chatter(asyncore.dispatcher): def writable( self): print "writable() - asyncore asked if we have data to write" return True def handle_write( self): print "handle_write() - asyncore asked us to write" self.send( "foobar") def handle_read( self): print "handle_read() - asyncore asked us to read" self.recv(1024) def handle_close( self): print "handle_close() - asyncore said the remote host closed connection" self.close() # we close here just like asyncore's example implementation def close( self): print "close() - we are closing our end of the connection" asyncore.dispatcher.close( self) def handle_error( self): print "handle_error() - asyncore exception: %s" % sys.exc_info()[1] self.close() # we close here just like asyncore's example implementation if __name__ == '__main__': listener( ('', 12345)) asyncore.loop( use_poll=True)