title: Using urllib2 via proxy
body: <p>I am trying to use <code>urllib2</code> through a proxy; however, after trying just about every variation of passing my verification details using <code>urllib2</code>, I either get a request that hangs forever and returns nothing or I get <code>407 Errors</code>. I can connect to the web fine using my browser which connects to a prox-pac and redirects accordingly; however, I can't seem to do anything via the command line <code>curl</code>, <code>wget</code>, <code>urllib2</code> etc. even if I use the proxies that the prox-pac redirects to. I tried setting my proxy to all of the proxies from the pac-file using <code>urllib2</code>, none of which work.</p>
  
  <p>My current script looks like this:</p>
  
  <pre><code>import urllib2 as url
  
  proxy = url.ProxyHandler({'http': 'username:password@my.proxy:8080'})
  auth = url.HTTPBasicAuthHandler()
  opener = url.build_opener(proxy, auth, url.HTTPHandler)
  url.install_opener(opener)
  url.urlopen("http://www.google.com/")
  </code></pre>
  
  <p>which throws <code>HTTP Error 407: Proxy Authentication Required</code> and I also tried:</p>
  
  <pre><code>import urllib2 as url
  
  handlePass = url.HTTPPasswordMgrWithDefaultRealm()
  handlePass.add_password(None, "http://my.proxy:8080", "username", "password")
  auth_handler = url.HTTPBasicAuthHandler(handlePass)
  opener = url.build_opener(auth_handler)
  url.install_opener(opener)
  url.urlopen("http://www.google.com")
  </code></pre>
  
  <p>which hangs like <code>curl</code> or <code>wget</code> timing out.</p>
  
  <p>What do I need to do to diagnose the problem? How is it possible that I can connect via my browser but not from the command line on the same computer using what would appear to be the same proxy and credentials?</p>
  
  <p>Might it be something to do with the router? if so, how can it distinguish between browser <code>HTTP</code> requests and command line <code>HTTP</code> requests?</p>