使用NTLM获取AttributeError的Python机械化:HTTPResponse实例没有属性'__iter';

使用NTLM获取AttributeError的Python机械化:HTTPResponse实例没有属性'__iter';,python,mechanize,ntlm,Python,Mechanize,Ntlm,我试图访问一个使用PythonNTLM和mechanize进行NTLM身份验证的站点,但我遇到了这个错误 File "build/bdist.macosx-10.6-universal/egg/mechanize/_mechanize.py", line 203, in open File "build/bdist.macosx-10.6-universal/egg/mechanize/_mechanize.py", line 249, in _mech_open File "build/bdi

我试图访问一个使用PythonNTLM和mechanize进行NTLM身份验证的站点,但我遇到了这个错误

File "build/bdist.macosx-10.6-universal/egg/mechanize/_mechanize.py", line 203, in open
File "build/bdist.macosx-10.6-universal/egg/mechanize/_mechanize.py", line 249, in _mech_open
File "build/bdist.macosx-10.6-universal/egg/mechanize/_mechanize.py", line 304, in _set_response
File "build/bdist.macosx-10.6-universal/egg/mechanize/_response.py", line 521, in upgrade_response
File "build/bdist.macosx-10.6-universal/egg/mechanize/_response.py", line 338, in __init__
File "build/bdist.macosx-10.6-universal/egg/mechanize/_response.py", line 353, in _set_fp
AttributeError: HTTPResponse instance has no attribute '__iter__'
当我使用urllib2库时,我能够得到正确的响应。但由于某些原因,当我尝试使用mechanize访问它时,它失败了

这是我的密码

import urllib2
from ntlm import HTTPNtlmAuthHandler

user = '<myusername>'
password = "<mypass>"
url = "https://somesite.com"

passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
passman.add_password(None, url, user, password)
# create the NTLM authentication handler
auth_NTLM = HTTPNtlmAuthHandler.HTTPNtlmAuthHandler(passman)

import mechanize
browser = mechanize.Browser()
handlersToKeep = []

for handler in browser.handlers:
    if not isinstance(handler,
    (mechanize._http.HTTPRobotRulesProcessor)):
        handlersToKeep.append(handler)

browser.handlers = handlersToKeep
browser.add_handler(auth_NTLM)

response = browser.open(url)
print(response.read())
导入urllib2
从ntlm导入HTTPNtlmAuthHandler
用户=“”
password=“”
url=”https://somesite.com"
passman=urllib2.HTTPPasswordMgrWithDefaultRealm()
密码(无、url、用户、密码)
#创建NTLM身份验证处理程序
auth\u NTLM=HTTPNtlmAuthHandler.HTTPNtlmAuthHandler(passman)
进口机械化
browser=mechanize.browser()
HandlerStokep=[]
对于browser.handlers中的处理程序:
如果不是isinstance(处理器,
(mechanize._http.HTTPRobotRulesProcessor)):
handlersToKeep.append(处理程序)
browser.handlers=handlersToKeep
browser.add\u处理程序(auth\u NTLM)
响应=浏览器。打开(url)
打印(response.read())

有人知道发生了什么事吗?我在这里做错了什么吗?

我修补了mechanize以解决这个问题:

--- _response.py.old    2013-02-06 11:14:33.208385467 +0100
+++ _response.py    2013-02-06 11:21:41.884081708 +0100
@@ -350,8 +350,13 @@
             self.fileno = self.fp.fileno
         else:
             self.fileno = lambda: None
-        self.__iter__ = self.fp.__iter__
-        self.next = self.fp.next
+
+        if hasattr(self.fp, "__iter__"):
+            self.__iter__ = self.fp.__iter__
+            self.next = self.fp.next
+        else:
+            self.__iter__ = lambda self: self
+            self.next = lambda self: self.fp.readline()

     def __repr__(self):
         return '<%s at %s whose fp = %r>' % (
--u response.py.old 2013-02-06 11:14:33.208385467+0100
+++_response.py 2013-02-06 11:21:41.884081708+0100
@@ -350,8 +350,13 @@
self.fileno=self.fp.fileno
其他:
self.fileno=lambda:None
-self.\uuuuu iter\uuuu=self.fp.\uuuuuu iter__
-self.next=self.fp.next
+
+如果hasattr(self.fp,“\uuuu iter””:
+self.\uuuuu iter\uuuu=self.fp.\uuuuuu iter__
+self.next=self.fp.next
+其他:
+self.\uuuu iter=lambda self:self
+self.next=lambda self:self.fp.readline()
定义报告(自我):
返回“”%(

…这似乎和thaks为您使用的代码所遇到的问题是一样的。我花了很长时间才弄清楚如何将NTLM与mechanize:-)嗯…这个补丁已经合并了吗?或者我必须自己修改文件来解决这个问题吗?不,不是,我只是在本地打了补丁。几天前我遇到了完全相同的问题。我用了你的方法,一切都很好。我还打开了mechanize并打开了一个pull请求。我已链接回此问题,并将修复归功于多维数据集。谢谢:-)您认为它会被合并吗?上次我检查的时候,Mechanize似乎死定了。对我来说似乎更活跃。看起来作者将其修补为与python3兼容。