title: Python urllib2 Basic Auth Problem
body: <p>Update: based on Lee's comment I decided to condense my code to a really simple script and run it from the command line:</p>
  
  <pre><code>import urllib2
  import sys
  
  username = sys.argv[1]
  password = sys.argv[2]
  url = sys.argv[3]
  print("calling %s with %s:%s\n" % (url, username, password))
  
  passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
  passman.add_password(None, url, username, password)
  urllib2.install_opener(urllib2.build_opener(urllib2.HTTPBasicAuthHandler(passman)))
  
  req = urllib2.Request(url)
  f = urllib2.urlopen(req)
  data = f.read()
  print(data)
  </code></pre>
  
  <p>Unfortunately it still won't generate the <code>Authorization</code> header (per Wireshark) :(</p>
  
  <p>I'm having a problem sending basic AUTH over urllib2. I took a look at <a href="http://www.voidspace.org.uk/python/articles/authentication.shtml" rel="noreferrer">this article</a>, and followed the example. My code:</p>
  
  <pre><code>passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
  passman.add_password(None, "api.foursquare.com", username, password)
  urllib2.install_opener(urllib2.build_opener(urllib2.HTTPBasicAuthHandler(passman)))
  
  req = urllib2.Request("http://api.foursquare.com/v1/user")    
  f = urllib2.urlopen(req)
  data = f.read()
  </code></pre>
  
  <p>I'm seeing the following on the Wire via wireshark:</p>
  
  <pre><code>GET /v1/user HTTP/1.1
  Host: api.foursquare.com
  Connection: close
  Accept-Encoding: gzip
  User-Agent: Python-urllib/2.5 
  </code></pre>
  
  <p>You can see the Authorization is not sent, vs. when I send a request via curl: <code>curl -u user:password http://api.foursquare.com/v1/user</code></p>
  
  <pre><code>GET /v1/user HTTP/1.1
  Authorization: Basic =SNIP=
  User-Agent: curl/7.19.4 (universal-apple-darwin10.0) libcurl/7.19.4 OpenSSL/0.9.8k zlib/1.2.3
  Host: api.foursquare.com
  Accept: */*
  </code></pre>
  
  <p>For some reason my code seems to not send the authentication - anyone see what I'm missing?</p>
  
  <p>thanks</p>
  
  <p>-simon</p>