Custom auth handler with mechanization

I would like to use python-ntlm with mechanize.Browser () I have an HTTPNtlmAuthHandler working with urllib2 and mechanize.urlopen () and tried to use it with Browser (), but it does not work.

Here is the code I use 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) 

Track On Demand

 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 
+4
source share
1 answer

There may be better options, but the only way to make it work is to remove the HTTPRobotRulesProcessor, which somehow prevented the HTTPNtlmAuthHandler from being called.

Note. The following code is also used to remove ProxyHandler, to bypass the proxy server - delete 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) 
+1
source

Source: https://habr.com/ru/post/1299629/


All Articles