title: Python: Non-blocking socket or Asynchronos I/O
body: <p>I am new to Python and currently have to write a python socket to be run as a script that communicates with a device over TCP/IP (a weather station).<br>
  The device acts as the Server Side (listening over IP:PORT, accepting connection, receiving request, transferring data).<br>
  I only need to send one message, receive the answer and then peacefully and nicely shutdown and close the socket.
  </p>
  
  <pre><code>try:
      comSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  except socket.error, msg:
      sys.stderr.write("[ERROR] %s\n" % msg[1])
      sys.exit(1)
  
  try:
     comSocket.connect((''))
  except socket.error, msg:
     sys.stderr.write("[ERROR] %s\n" % msg[1])
     sys.exit(2)
  
  comSocket.send('\r')
  comSocket.recv(128)
  comSocket.send('\r')
  comSocket.recv(128)
  comSocket.send('\r\r')
  comSocket.recv(128)
  
  comSocket.send('1I\r\r3I\r\r4I\r\r13I\r\r5I\r\r8I\r\r7I\r\r9I\r\r')
  
  rawData = comSocket.recv(512)
  
  comSocket.shutdown(1)
  comSocket.close()
  </code></pre>
  
  <p><strong>The problem I'm having is:</strong><br>
  The communication channel is unreliable, the device is slow. So, sometimes the device response with message of length 0 (just an ACK), the my code will freeze and wait for response forever.<br>
  This piece of code contains the portion that involves SOCKET, the whole code will be run under CRON so freezing is not a desirable behavior.  </p>
  
  <p><strong>My question is:</strong><br>
  What would be the best way in Python to handle that behavior, so that the code doesn't freeze and wait forever but will attempt to move on to the next send (or such).</p>