自定义auth处理程序与机械化

时间:2010-01-28 08:07:09

标签: python mechanize

我想使用python-ntlm和mechanize.Browser()我有HTTPNtlmAuthHandler使用urllib2和mechanize.urlopen()并尝试使用它与Browser()但它不起作用

以下是我用于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)

按要求追溯

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

1 个答案:

答案 0 :(得分:1)

可能有更好的选择,但我能让它工作的唯一方法是删除HTTPRobotRulesProcessor处理程序,这会阻止HTTPNtlmAuthHandler被调用。

注意:以下代码也会删除ProxyHandler以绕过代理服务器 - 如果适用,请删除。

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)