title: Handling authentication and proxy servers with httplib2
body: <p>I'm attempting to test interactions with a Nexus server that requires authentication for the operations I intend to use, but I also need to handle an internal proxy server.</p>
  
  <p>Based on my (limited) understanding I can add multiple handlers to the opener. However I'm still getting a 401 response. I've checked the username and password are valid. I'm not certain if cookies are required to do this and if so how they'd be included.
  Any suggestions?</p>
  
  <pre><code>baseUrl = 'server:8070/nexus-webapp-1.3.3/service/local'
  
  params = {"[key]":"[value]"}
  
  data = urllib.urlencode(params)
  
  # create a password manager
  password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
  
  # Add the username and password as supplied
  password_mgr.add_password(None, baseUrl, username, password)
  
  handler = urllib2.HTTPBasicAuthHandler(password_mgr)
  proxy_support = urllib2.ProxyHandler({})
  
  # create "opener" (OpenerDirector instance)
  opener = urllib2.build_opener(proxy_support, handler)
  urllib2.install_opener(opener)
  
  txheaders =  {'User-agent' : 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'}
  
  req = Request(protocol+url, data, txheaders)
  handle = urlopen(req)
  </code></pre>
  
  <p>This is the resulting URLError's headers field:</p>
  
  <pre><code>&gt;HTTPMessage: Server: Apache-Coyote/1.1
  Set-Cookie: JSESSIONID=B4BD05C9582F7B27495CBB675A339724; Path=/nexus-webapp-1.3.3
  WWW-Authenticate: NxBASIC realm="Sonatype Nexus Repository Manager API"
  Content-Type: text/html;charset=utf-8
  Content-Length: 954
  Date: Fri, 03 Jul 2009 17:38:42 GMT
  Connection: close
  </code></pre>
  
  <p><strong>Update</strong>
  It seems Nexus implement a custom version of <a href="http://svn.sonatype.org/nexus/tags/nexus-1.3.4/nexus-clients/nexus-rest-client-java/src/main/java/org/sonatype/nexus/client/rest/HttpNxBasicHelper.java" rel="nofollow noreferrer">Restlet's AuthenticationHelper</a>. Thanks to Alex I knew what to look for.</p>