title: Python script hangs in loop
body: <p>In one of my scripts I'm using the following infinite loop to check for an active internet connection:</p>
  
  <pre><code>def online_check():
      try:
          con = urllib2.urlopen("http://www.google.com/")
          data = con.read()
          logging.debug('{0} Reached the host. Exiting online_check'.format(time.strftime('[ %H:%M:%S ]')))
      except:
          logging.debug('{0} Could not reach host trying again in 3 seconds'.format(time.strftime('[ %H:%M:%S ]')))
          time.sleep(3)
          online_check()
  </code></pre>
  
  <p>I am aware that this is not a very elegant solution but the problem is that when I start my script it will sometimes call the online_check method and get stuck in the middle of it (Once in roughly 200 attempts). The script is still running and no exception is thrown; the script is just stuck. I can press CTRL+C (Even after hours of the script being stuck) and it will just throw an exception and continue with the next online_check. I also rewrote the script to check for an IP address in 'ifconfig' as opposed to pinging google, unfortunately with similar results.</p>
  
  <p>What am I doing wrong? Can I rewrite the script so this never happens? Is there something I can do to find out what is happening here? </p>
  
  <p>Help is greatly appreciated. Btw. I'm using Python2.7.1 and I've tried this script both on Linux and Mac.</p>
  
  <p>P.S: If you have recommendations for designing a method that checks for connectivity without using bandwidth and minimal overhead, I'd be more than happy to hear it. </p>