Python SUDS SOAP请求HTTPS服务401错误

6 投票
2 回答
13387 浏览
提问于 2025-04-17 09:22

我正在尝试使用SUDS,但卡在了无法进行身份验证(或者说https)的问题上。

我想访问的服务是通过https并使用基本的摘要身份验证。根据调试信息,似乎它在使用http而不是https。但我不太确定我遗漏了什么。如果有任何线索,我会很感激。

from suds.client import Client
from suds.transport.http import HttpAuthenticated
import logging
logging.basicConfig(level=logging.DEBUG)
logging.getLogger('suds.client').setLevel(logging.DEBUG)
logging.getLogger('suds.transport').setLevel(logging.DEBUG)
logging.getLogger('suds.xsd.schema').setLevel(logging.DEBUG)
logging.getLogger('suds.wsdl').setLevel(logging.DEBUG)

def main():
    url = 'https://blah.com/soap/sp/Services?wsdl'
    credentials = dict(username='xxxx', password='xxxx')
    t = HttpAuthenticated(**credentials)
    client = Client(url, location='https://blah.com/soap/sp/Services', transport=t)
    print client.last_sent()

if __name__=="__main__":
    main()

调试输出:

DEBUG:suds.wsdl:正在读取wsdl,地址是: https://blah.com/soap/sp/Services?wsdl ... DEBUG:suds.transport.http:正在打开 (https://blah.com/soap/sp/Services?wsdl)
省略部分 ...
文件 "C:\Python27\Lib\site-packages\suds-0.4-py2.7\suds\reader.py",第95行,在下载中
fp = self.options.transport.open(Request(url))

文件 "C:\Python27\Lib\site-packages\suds-0.4-py2.7\suds\transport\http.py",第173行,在打开中
return HttpTransport.open(self, request)

文件 "C:\Python27\Lib\site-packages\suds-0.4-py2.7\suds\transport\http.py",第64行,在打开中
引发 TransportError(str(e), e.code, e.fp)

suds.transport.TransportError: HTTP错误 401:需要授权

2 个回答

5

我遇到了这个问题,找到了一种对我有效的解决办法。我的服务器使用的是NTLM认证,所以为了让suds能正常工作,我只需要按照文档中的“Windows (NTLM)”部分来操作。

首先,安装python-ntlm,然后你就可以写:

from suds.transport.https import WindowsHttpAuthenticated
ntlm = WindowsHttpAuthenticated(username='xx', password='xx')
client = Client(url, transport=ntlm)
7

Suds提供了两个叫做HttpAuthenticated的类,一个在模块里,另一个在suds.transport.https模块里。看起来你是从suds.transport.http这个模块创建的实例,但因为你的网址是以https://开头的,所以你可能想试试suds.transport.https.HttpAuthenticated

撰写回答