title: Detecting hangs with Python urllib2.urlopen
body: <p>I'm using <a href="http://docs.python.org/library/urllib2.html" rel="noreferrer">Python's urllib2</a> to send an HTTP post:</p>
  
  <pre><code>import socket, urllib, urllib2
  
  socket.setdefaulttimeout(15)    
  
  postdata = urllib.urlencode({'value1' : 'a string', 'value2' : 'another string'})
  headers = {
      'User-Agent': 'Agent',
      'Content-Type': 'application/x-www-form-urlencoded',
      'Accept': 'text/html, */*',
  }
  
  try: 
      request = urllib2.Request('http://www.example.com', postData, headers)
      response = urllib2.urlopen(request)
  except urllib2.HTTPError, e:
      # Handle here
  except urllib2.URLError, e:
      # Handle here
  except httplib.HTTPException, e:
      # Handle here
  </code></pre>
  
  <p>Occasionally a network issue results in the call to urlopen never returning. We see other errors (including timeouts) handled correctly by the except block and have a call to socket.setdefaulttimeout() but there are still instances where the urlopen will never return.</p>
  
  <p>I know it's never returning because we have some log lines in our actual code which get called before and after, and when this problem occurs only the calls before are made and the script hangs forever.</p>
  
  <p>What's the best way to detect/handle this? </p>