title: using select() method for client/ server chat in Python
body: <p>I am writing a client/ server program in Python where, once the client and server have successfully connected via a socket, they may exchange messages. Below is my server and client code. When compiled, the connection is established correctly and the messages are sent successfully, but one cannot send a second message until it has received a response from the other party.</p>
  <p>For example:</p>
  <p>Client sends: &quot;Hello, server!&quot;</p>
  <p>Server sends: &quot;I have received your message, client!&quot;</p>
  <p>Client sends: &quot;great, here's another one&quot;</p>
  <p>Client sends: &quot;and a second one!&quot;</p>
  <p>At this point, the server terminal window has received the message saying &quot;great, here's another one&quot;, but must first reply to this message before receiving &quot;and a second one!&quot;.
  I think my issue is that I need to use the select() method, but do not understand how to do so. How can I fix this?</p>
  <pre class="lang-py prettyprint-override"><code>#The server code
  
  HOST = '' 
  PORT = 9999
  
  s = socket(AF_INET, SOCK_STREAM)
  s.bind((HOST, PORT))
  print(&quot;Now listening...&quot;)
  s.listen(1) #only needs to receive one connection (the client)
  conn, addr = s.accept() #accepts the connection
  print(&quot;Connected by: &quot;, addr) #prints the connection
  i = True
  
  while i is True:
      data = conn.recv(1024) #receives data
      print('Received:' , repr(data)) #prints the message from client 
      
      reply = raw_input() #server types a response
      conn.sendall(reply) #server now sends response back to client
      
  close()
  </code></pre>
  <p>below is the client code (client.py)</p>
  <h1>The client code</h1>
  <pre><code>from socket import*
  
  HOST = '192.168.41.1'
  PORT = 9999
  s = socket(AF_INET, SOCK_STREAM)
  s.connect((HOST, PORT))
  
  while True:
      message = raw_input()  #client's message to the server
      s.send(message) #sends message to the server
      print(&quot;Waiting for response...&quot;) 
      reply = s.recv(1024) #receives message from server
      print(&quot;New message: &quot; + repr(reply)) #prints the message received 
  
  close()
  </code></pre>