Nonblocking client-server in python
Question: I have been coding a blocking client-server in python. how can I change it to
nonblocking without thread?


Answer: Check out the [asyncore](http://docs.python.org/2/library/asyncore.html) and
[asynchat](http://docs.python.org/2/library/asynchat.html) Python modules.
Here's a basic echo server (taken from the asyncore documentation):

    
    
    import asyncore
    import socket
    
    class EchoHandler(asyncore.dispatcher_with_send):
    
        def handle_read(self):
            data = self.recv(8192)
            if data:
                self.send(data)
    
    class EchoServer(asyncore.dispatcher):
    
        def __init__(self, host, port):
            asyncore.dispatcher.__init__(self)
            self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
            self.set_reuse_addr()
            self.bind((host, port))
            self.listen(5)
    
        def handle_accept(self):
            pair = self.accept()
            if pair is not None:
                sock, addr = pair
                print 'Incoming connection from %s' % repr(addr)
                handler = EchoHandler(sock)
    
    server = EchoServer('localhost', 8080)
    asyncore.loop()
    

