title: Python sockets, how to escape from infinite loop and handle exceptions
body: <p>I have a script that connects to a remote server. The code is below</p>
  
  <pre><code>s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  s.connect((remote_host,remote_port))
  s.setblocking(False)
  
  
  while True:
      try:
          data = s.recv(1024)
  
          if not data:
             break
  
          pkt_type = ord(data[2]) # get pkt type
  
          if pkt_type == Reset:
             s.send(data)
  
          if pkt_type == Authenticate:
             processAuthenticate(s,data)
             break
  
      except:
          pass
  
  while(True)
   .
   .
   .
  </code></pre>
  
  <p>I wait for reset and echo back to the server, then wait for an Authenticate packet, twiddle a few bit and echo that back to the server. Once this is done successfully I can now request data from the the server. This is done in the next while(true) loop.</p>
  
  <p>Is this the best way of doing this. Sometimes when I run the script I get an error, what is the simplest way of handling the exception and preventing execuation of the next wile loop?</p>
  
  <p>Thanks</p>