title: Use python mechanize to log into pages with NTLM authentication
body: <p>I want to use mechanize to log into a page and retrieve some information. But however I try to authenticate It just fails with Error code <strong>HTTP 401</strong>, as you can see below:</p>
  
  <pre><code>r = br.open('http://intra')
  File "bui...e\_mechanize.py", line 203, in open
  File "bui...g\mechanize\_mechanize.py", line 255,
  in _mech_openmechanize._response.httperror_seek_wrapper: HTTP Error 401: Unauthorized
  </code></pre>
  
  <p>This is my code so far:</p>
  
  <pre><code>import mechanize
  import cookielib
  
  # Browser
  br = mechanize.Browser()
  
  # Cookie Jar
  cj = cookielib.LWPCookieJar()
  br.set_cookiejar(cj)
  
  # Browser options
  br.set_handle_equiv(True)
  # br.set_handle_gzip(True)
  br.set_handle_redirect(True)
  br.set_handle_referer(True)
  br.set_handle_robots(False)
  
  # Follows refresh 0 but not hangs on refresh &gt; 0
  br.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(), max_time=1)
  
  # If the protected site didn't receive the authentication data you would
  # end up with a 410 error in your face
  br.add_password('http://intra', 'myusername', 'mypassword')
  
  # User-Agent (this is cheating, ok?)
  br.addheaders = [('User-agent', 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.1) Gecko/2008071615 Fedora/3.0.1-1.fc9 Firefox/3.0.1')]
  # Open some site, let's pick a random one, the first that pops in mind:
  # r = br.open('http://google.com')
  r = br.open('http://intra')
  html = r.read()
  
  # Show the source
  print html
  </code></pre>
  
  <p>What am I doing wrong? visiting <code>http://intra</code> (internal page) with e.g. chrome, it pops open a windows and asks for username/password once and then all is good.</p>
  
  <p>The dialogue which pops open looks like this:</p>
  
  <p><img src="https://i.stack.imgur.com/JTb7M.png" alt="enter image description here"></p>