title: How to catch 404 error in urllib.urlretrieve
body: <p>Background: I am using <a href="http://docs.activestate.com/activepython/2.6/python/library/urllib.html" rel="noreferrer"><code>urllib.urlretrieve</code></a>, as opposed to any other function in the <code>urllib*</code> modules, because of the hook function support (see <code>reporthook</code> below) .. which is used to display a textual progress bar. This is Python >=2.6.</p>
  
  <pre><code>&gt;&gt;&gt; urllib.urlretrieve(url[, filename[, reporthook[, data]]])
  </code></pre>
  
  <p>However, <code>urlretrieve</code> is so dumb that it leaves no way to detect the status of the HTTP request (eg: was it 404 or 200?).</p>
  
  <pre><code>&gt;&gt;&gt; fn, h = urllib.urlretrieve('http://google.com/foo/bar')
  &gt;&gt;&gt; h.items() 
  [('date', 'Thu, 20 Aug 2009 20:07:40 GMT'),
   ('expires', '-1'),
   ('content-type', 'text/html; charset=ISO-8859-1'),
   ('server', 'gws'),
   ('cache-control', 'private, max-age=0')]
  &gt;&gt;&gt; h.status
  ''
  &gt;&gt;&gt;
  </code></pre>
  
  <p>What is the best known way to download a remote HTTP file with hook-like support (to show progress bar) and a decent HTTP error handling?</p>