10

背景

我正在用 python 2.7.3 构建一个 SOAP 客户端,并使用 Canonical 提供的 suds 0.4.1 库。服务器正在通过 HTTPS 使用基本身份验证。

问题

无法通过服务器上的身份验证,甚至无法访问 WSDL。我收到以下错误:

suds.transport.TransportError:HTTP 错误 401:未经授权

尝试解析和代码

我已经尝试了suds 文档中描述的两种身份验证方法,但仍然出现上述错误client = Client(url, ...)。我已经确认了在网络浏览器中连接的凭据和能力,这工作正常。

在声明wsdl_url, usernameand之后password,我尝试了:

client = Client(url=wsdl_url, username=username, password=password)

# as well as:

t = HttpAuthenticated(username=username, password=password)
client = Client(url=wsdl_url, transport=t)

# and even:

t = HttpAuthenticated(username=username, password=password)
t.handler = urllib2.HTTPBasicAuthHandler(t.pm)
t.urlopener = urllib2.build_opener(t.handler)
client = Client(url=wsdl_url, transport=t)

最后一个似乎至少从 WSDL URL 中得到了另一个关于 HTTP authentication with suds的问题的响应。

其他注意事项

这个问题与这个类似的问题不同,因为我使用的是:

from suds.transport.https import HttpAuthenticated
# not:
# from suds.transport.http import HttpAuthenticated

并且从 Traceback 中,client = Client(url, ...)电话明显命中 suds.transport.https.py:

File "/usr/lib/python2.7/dist-packages/suds/client.py", line 112, in __init__ self.wsdl = reader.open(url)
File "/usr/lib/python2.7/dist-packages/suds/reader.py", line 152, in open d = self.fn(url, self.options)
File "/usr/lib/python2.7/dist-packages/suds/wsdl.py", line 136, in __init__ d = reader.open(url)
File "/usr/lib/python2.7/dist-packages/suds/reader.py", line 79, in open d = self.download(url)
File "/usr/lib/python2.7/dist-packages/suds/reader.py", line 95, in download fp = self.options.transport.open(Request(url))
File "/usr/lib/python2.7/dist-packages/suds/transport/https.py", line 60, in open return  HttpTransport.open(self, request)
File "/usr/lib/python2.7/dist-packages/suds/transport/http.py", line 64, in open raise TransportError(str(e), e.code, e.fp)

我错过了什么?

4

2 回答 2

11

suds 没有将授权标头添加到请求中,因此我手动设置了它:

import base64

# code excluded for brevity

base64string = base64.encodestring('%s:%s' % (username, password)).replace('\n', '')
authenticationHeader = {
    "SOAPAction" : "ActionName",
    "Authorization" : "Basic %s" % base64string
}
client = Client(url=wsdl_url, headers=authenticationHeader)
于 2012-08-28T16:00:56.763 回答
0

您可以使用 SOAPpy

import SOAPpy, base64

class myHTTPTransport(SOAPpy.HTTPTransport):
    username = None
    passwd = None

    @classmethod
    def setAuthentication(cls,u,p):
        cls.username = u
        cls.passwd = p

    def call(self, addr, data, namespace, soapaction=None, encoding=None, http_proxy=None, config=SOAPpy.Config, timeout=None):

        if not isinstance(addr, SOAPpy.SOAPAddress):
          addr = SOAPpy.SOAPAddress(addr, config)

        if self.username != None:
          addr.user = self.username+":"+self.passwd

        return SOAPpy.HTTPTransport.call(self, addr, data, namespace, soapaction, encoding, http_proxy, config)


if __name__ == '__main__':

  # code for authenticating the SOAP API calls
  myHTTPTransport.setAuthentication('admin', 'admin')

  # Getting the instance of Server
  Baton = SOAPpy.WSDL.Proxy(<WSDL PATH>, transport=myHTTPTransport)

  # your code goes here
  ...
于 2014-02-13T18:55:52.573 回答