使用sudsoap库进行HTTP身份验证时的奇怪行为

2024-05-15 00:40:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个正在工作的python程序,它使用sud通过SOAP获取大量数据。web服务是用一个分页函数实现的,这样我就可以在每个fetch调用中获取nnn行,并在随后的调用中获取下一个nnn行。如果我使用如下代码对HTTP服务器进行身份验证

client = suds.client.Client(url=url, location=location, username=username, password=password, timeout=timeout)

一切都很好。但是,如果我使用

t = suds.transport.https.HttpAuthenticated(username=username, password=password)
t.handler = urllib2.HTTPBasicAuthHandler(t.pm)
t.urlopener = urllib2.build_opener(t.handler)
client = suds.client.Client(url=url, location=location, timeout=timeout, transport=t) 

它只工作了6次迭代。如果我指定每次取10行的取数限制,则返回60行。第七次取货时,我收到

  File "build/bdist.linux-i686/egg/suds/client.py", line 542, in __call__
  File "build/bdist.linux-i686/egg/suds/client.py", line 602, in invoke
  File "build/bdist.linux-i686/egg/suds/client.py", line 649, in send
  File "build/bdist.linux-i686/egg/suds/client.py", line 698, in failed
AttributeError: 'NoneType' object has no attribute 'read'

有人对可能导致这种情况的原因有什么建议吗。肯定是这种变化造成了这个问题。我可以前后交换身份验证样式,它是完全可复制的。

我正在用sud0.4运行python 2.6.6。

谢谢


Tags: pybuildclienturlegglinuxlinetimeout
1条回答
网友
1楼 · 发布于 2024-05-15 00:40:23

问题似乎是一个urllib2.HTTPError正在从较低的级别提升,其fp属性为None:

suds.transport.http中的第81行:

except u2.HTTPError, e:
    if e.code in (202,204):
        result = None
    else:
        raise TransportError(e.msg, e.code, e.fp)

这个异常最终被传递到suds.client中的第698行,其中error.fp.read()行爆炸:

def failed(self, binding, error):
    status, reason = (error.httpcode, tostr(error))
    reply = error.fp.read()

我建议monkey修补suds.SoapClient类以获取HTTP错误代码和消息。在构造suds.Client之前添加这些行,然后运行它以查看第7次提取引发的HTTP错误:

class TestClient(suds.client.SoapClient):
    def failed(self, binding, error):
        print error.httpcode, error.args

suds.client.SoapClient = TestClient

相关问题 更多 >

    热门问题