我正在尝试使用suds访问使用Python的Web服务。这是我的代码:
# WSDL fails to import schema, import this and create client.
imp = Import('http://schemas.xmlsoap.org/soap/encoding/')
imp.filter.add('http://api.daisycon.com/publisher/soap//program/')
d = ImportDoctor(imp)
encoded = base64.b64encode("%s:%s" % (username, password))
authenticationHeader = {
"SOAPAction" : "http://api.daisycon.com/publisher/soap//program/#getSubscriptions",
"Authorization" : "Basic %s" % encoded
}
client = suds.client.Client(self.url, headers = authenticationHeader, doctor=d)
但是,调用方法时会产生以下错误:
No handlers could be found for logger "suds.client"
Traceback (most recent call last):
File "C:\Users\User\Documents\Crawler\src\Crawlers\Daisycon.py", line 39, in <module>
Crawler().main()
File "C:\Users\User\Documents\Crawler\src\Crawlers\Daisycon.py", line 37, in main
print client.service.getSubscriptions()
File "C:\Python27\Lib\site-packages\suds\client.py", line 542, in __call__
return client.invoke(args, kwargs)
File "C:\Python27\Lib\site-packages\suds\client.py", line 602, in invoke
result = self.send(soapenv)
File "C:\Python27\Lib\site-packages\suds\client.py", line 649, in send
result = self.failed(binding, e)
File "C:\Python27\Lib\site-packages\suds\client.py", line 708, in failed
raise Exception((status, reason))
Exception: (401, u'Unauthorized')
我尝试过在网络上找到的各种方式添加凭据。什么都行不通。我100%肯定凭证是对的。
我尝试的另一种方式:
t = HttpAuthenticated(username=username,
password=password)
client = suds.client.Client(self.url, transport=t, doctor=d)
导致:
No handlers could be found for logger "suds.client"
Traceback (most recent call last):
File "C:\Users\User\Documents\Crawler\src\Crawlers\Daisycon.py", line 35, in <module>
Crawler().main()
File "C:\Users\User\Documents\Crawler\src\Crawlers\Daisycon.py", line 33, in main
print client.service.getFeeds()
File "C:\Python27\Lib\site-packages\suds\client.py", line 542, in __call__
return client.invoke(args, kwargs)
File "C:\Python27\Lib\site-packages\suds\client.py", line 602, in invoke
result = self.send(soapenv)
File "C:\Python27\Lib\site-packages\suds\client.py", line 649, in send
result = self.failed(binding, e)
File "C:\Python27\Lib\site-packages\suds\client.py", line 702, in failed
r, p = binding.get_fault(reply)
File "C:\Python27\Lib\site-packages\suds\bindings\binding.py", line 265, in get_fault
raise WebFault(p, faultroot)
suds.WebFault: Server raised fault: 'Need authorisation'
答案 0 :(得分:0)
也许用户名/密码作为soapheaders传递?如果是这样,你就这样传递它们:
from suds.sax.attribute import Attribute
from suds.sax.element import Element
...
usr = Element('USER').setText('username')
pwd = Element('PASSWORD').setText('password')
header_list = [usr, pwd]
reqsoap_attribute = Attribute('xsi:type', "xsd:string")
for param in header_list:
param.append(reqsoap_attribute)
client.set_options(soapheaders=header_list)
这会在信封中添加soap:Header部分,例如:
<soap:Header>
<USER xsi:type="xsd:string">username</USER>
<PASSWORD xsi:type="xsd:string">password</PASSWORD>
</soap:Header>