import asyncore import socket class async_hello(asyncore.dispatcher_with_send): def __init__(self, host, port=9090): asyncore.dispatcher_with_send.__init__(self) self.host = host self.port = port self.create_socket(socket.AF_INET, socket.SOCK_STREAM) self.connect((host, port)) def __repr__(self): return "" % (self.host, self.port) def handle_connect(self): self.send("hello world") def handle_expt(self): print "get exception" self.close() def handle_close(self): self.close() def test(): async_hello('localhost') asyncore.loop(use_poll=True) if __name__ == "__main__": test()