title: Python mechanize implementation of HTTP Basic Auth
body: <p>I could get HTTP Basic Authentication to work using requests:</p>
  
  <pre><code>import requests
  request = requests.post(url, auth=(user, pass), data={'a':'whatever'})
  </code></pre>
  
  <p>And also using urllib2 and urllib:</p>
  
  <pre><code>import urllib2, urllib
  passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
  passman.add_password(None, url, user, pass)
  auth_handler = urllib2.HTTPBasicAuthHandler(passman)
  opener = urllib2.build_opener(auth_handler)
  urllib2.install_opener(opener)
  content = urllib2.urlopen(url, urllib.urlencode({'a': 'whatever'}))
  </code></pre>
  
  <p>The problem is I get an unauthorized error when I try the same thing with mechanize:</p>
  
  <pre><code>import mechanize, urllib
  from base64 import b64encode
  
  browser = mechanize.Browser()
  b64login = b64encode('%s:%s' % (user, pass))
  browser.addheaders.append(('Authorization', 'Basic %s' % b64login ))
  request = mechanize.Request(url)
  response = mechanize.urlopen(request, data=urllib.urlencode({'a':'whatever}))
  </code></pre>
  
  <p>error:</p>
  
  <pre><code>HTTPError: HTTP Error 401: UNAUTHORIZED
  </code></pre>
  
  <p>The code I tried with mechanize could be trying to authenticate in a different way than the other two code snippets. So the question is how could the same authentication process be achieved in mechanize.
  I am using python 2.7.12</p>