首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用NTLM获得AttributeError的Python机械化: HTTPResponse实例没有属性'__iter__‘

使用NTLM获得AttributeError的Python机械化: HTTPResponse实例没有属性'__iter__‘
EN

Stack Overflow用户
提问于 2012-12-01 01:19:00
回答 1查看 1.5K关注 0票数 2

我试图访问一个使用python-ntlm和mechanize的NTLM身份验证安全的站点,但是我得到了这个错误。

代码语言:javascript
复制
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访问它时,它失败了。

这是我拥有的代码。

代码语言:javascript
复制
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())

有人知道这是怎么回事吗?我是不是做错了什么?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-02-06 18:26:59

我打了补丁来解决这个问题:

代码语言:javascript
复制
--- _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>' % (
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13649964

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档