title: How to run a background procedure while constantly checking for input - threading?
body: <p>I have small server and client Python scripts where the client sends a string and the server responds with the reverse. When the client enters a quit string, the client exits and then the server exits.</p>
  
  <p>I want the server's "receive, reverse, send" procedure running in the background while the program is constantly checking stdin for a quit string.</p>
  
  <p>I've tried using <code>threading</code> but because of the blocking that many socket calls cause it wouldn't work properly.</p>
  
  <p>Just so you can get an idea of what I've already done.</p>
  
  <p><em>server.py</em>:</p>
  
  <pre><code>import socket
  from time import sleep
  
  sock = socket.socket()
  sock.bind(("127.0.0.1",12346))
  sock.listen(3)
  print "Waiting on connection"
  conn = sock.accept() 
  print "Client connected"
  
  while True:
      m = conn[0].recv(4096)
      if m == "exit":
          sleep(1)
          break
      else:
          conn[0].send(m[::-1])
  
  sock.shutdown(socket.SHUT_RDWR)
  sock.close()
  </code></pre>
  
  <p><em>client.py</em>:</p>
  
  <pre><code>import socket
  
  sock = socket.socket()
  sock.connect(("127.0.0.1",12346))
  
  while True:
      s = raw_input("message: ")
      sock.send(s)
  
      if s == "exit":
          print "Quitting"
          break
  
      print sock.recv(4096)
  
  sock.shutdown(socket.SHUT_RDWR)
  sock.close()
  </code></pre>