Reputation: 303
I would like to use python-ntlm with mechanize.Browser() I have got HTTPNtlmAuthHandler working with urllib2 and mechanize.urlopen() and tried to use it with Browser() but it doesn't work
Here is the code I'm using for urlopen
passman = mechanize.HTTPPasswordMgrWithDefaultRealm()
passman.add_password(None, url, user, password)
auth_NTLM = HTTPNtlmAuthHandler.HTTPNtlmAuthHandler(passman)
opener = mechanize.build_opener(auth_NTLM)
mechanize.install_opener(opener)
mechanize.urlopen(baseurl)
Traceback as requested
harrisony@lithium:~$ python sitefoo.py
now running mechanize.urlopen
<addinfourl at 169181868 whose fp = <httplib.HTTPResponse instance at 0xa15858c>>
now running mechanize.Browser then br.open
Traceback (most recent call last):
File "sitescreaper.py", line 21, in <module>
br.open(baseurl)
File "/usr/lib/python2.6/dist-packages/mechanize/_mechanize.py", line 209, in open
return self._mech_open(url, data, timeout=timeout)
File "/usr/lib/python2.6/dist-packages/mechanize/_mechanize.py", line 261, in _mech_open
raise response
mechanize._response.httperror_seek_wrapper: HTTP Error 401: Unauthorized
Upvotes: 4
Views: 2569
Reputation: 21
There may be better options, but the only way I could get it to work was to remove the HTTPRobotRulesProcessor handler which was somehow preventing the HTTPNtlmAuthHandler from being called.
Note: The following code also happens to remove the ProxyHandler in order to bypass the proxy server - remove if applicable.
passman = mechanize.HTTPPasswordMgrWithDefaultRealm()
passman.add_password(None, baseurl, user, password)
auth_NTLM = HTTPNtlmAuthHandler.HTTPNtlmAuthHandler(passman)
browser = mechanize.Browser()
browser.add_handler(auth_NTLM)
handlersToKeep = []
for handler in browser.handlers:
if not isinstance(handler, (mechanize._auth.ProxyHandler,
mechanize._urllib2_support.HTTPRobotRulesProcessor)):
handlersToKeep.append(handler)
browser.handlers = handlersToKeep
browser.open(url)
Upvotes: 2