Python使用NTLM机械化,得到AttributeError:HTTPResponse实例没有属性'__iter__' [英] Python mechanize with NTLM getting AttributeError: HTTPResponse instance has no attribute '__iter__'
本文介绍了Python使用NTLM机械化,得到AttributeError:HTTPResponse实例没有属性'__iter__'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用python-ntlm访问由NTLM身份验证保护的网站并进行机械化,但是出现此错误.
I am trying to access a site that's secured with NTLM authentication using python-ntlm and mechanize but I am getting this error.
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库时,我可以得到正确的响应.但是由于某种原因,当我尝试使用机械化访问它时,它会失败.
I am able to get a proper response when I use the urllib2 library. But for some reason, it fails when I try to access it using 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())
有人知道发生了什么吗?我在这里做错了吗?
Does anyone has any idea what's going on? Am I doing something wrongly here?
推荐答案
我修补了机械化程序,以解决此问题:
I patched mechanize to work around this:
--- _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>' % (
这篇关于Python使用NTLM机械化,得到AttributeError:HTTPResponse实例没有属性'__iter__'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文