6

我有一个正在运行的 python 程序,它使用 suds 通过 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(用户名=用户名,密码=密码)
t.handler = urllib2.HTTPBasicAuthHandler(t.pm)
t.urlopener = urllib2.build_opener(t.handler)
客户端 = suds.client.Client(url=url, location=location, timeout=timeout, transport=t)

它恰好适用于 6 次迭代。也就是说,如果我指定每次提取 10 行的提取限制,我会返回 60 行。在第七次取货时,我收到

  __call__ 中的文件“build/bdist.linux-i686/egg/suds/client.py”,第 542 行
  调用中的文件“build/bdist.linux-i686/egg/suds/client.py”,第 602 行
  文件“build/bdist.linux-i686/egg/suds/client.py”,第 649 行,在发送中
  文件“build/bdist.linux-i686/egg/suds/client.py”,第 698 行,失败
AttributeError:“NoneType”对象没有“读取”属性

有没有人对可能导致这种情况的原因有任何建议。绝对是这种变化导致了问题。我可以来回交换身份验证样式,它是完全可重现的。

我正在使用 suds 0.4 运行 python 2.6.6。

谢谢

4

1 回答 1

10

问题似乎urllib2.HTTPError是从较低级别提出的,其fp属性为None:

第 81 行suds.transport.http

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

该异常最终被传递到第 698 行,suds.client在该error.fp.read()行爆炸的地方:

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

我建议猴子修补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
于 2011-03-07T05:57:00.557 回答