[英]Python Suds Soap Client Error
我正在嘗試使用Python / SUDS連接到Web服務。
我在單個文件中包含以下代碼,並且能夠成功連接並且收到響應。
class Suds_Connect:
def __init__(self, url, q_user, q_passwd):
logging.basicConfig(level=logging.INFO)
logging.getLogger('suds.client').setLevel(logging.DEBUG)
try:
# fix broken wsdl
# add <s:import namespace="http://www.w3.org/2001/XMLSchema"/> to the wsdl
imp = Import('http://www.w3.org/2001/XMLSchema',
location='http://www.w3.org/2001/XMLSchema.xsd')
wsdl_url = url
self.client = Client(wsdl_url, doctor=ImportDoctor(imp))
t = HttpAuthenticated()
security = Security()
token = UsernameToken(q_user,q_passwd)
security.tokens.append(token)
self.client.set_options(wsse=security)
except Exception, e:
print "Unexpected error:", sys.exc_info()[0]
print str(e)
sys.exit()
def CallWebMethod():
try:
print ' SUDS Client'
print self.client
Person= self.client.factory.create('ns0:Person')
Person.name= 'bob'
Person.age= '34'
Person.address= '44, river lane'
print self.client.service.AddPerson(Person)
except WebFault, f:
print str(f.fault)
except Exception, e:
print str(e)
if __name__ == '__main__':
errors = 0
sudsClient = Suds_Connect('url','user','password')
sudsClient.CallWebMethod()
print '\nFinished:'
我想在將從按鈕單擊事件中調用的Python客戶端應用程序中使用此代碼。 我已經嘗試實現此功能,並且能夠打印出客戶端,但是當我進行Web服務調用( print self.client.service.AddPerson(Person)
)時,出現以下錯誤。
unsupported operand type(s) for +: 'NoneType' and 'str'
我該如何解決該錯誤?
看起來Web服務調用返回None。 需要其他信息來確定出了什么問題。
首先-通過在調用Suds_Connect之前添加來啟用suds日志記錄。 這應該為您提供有關引擎蓋下肥皂水發生情況的信息。
import logging
logging.basicConfig(level=logging.DEBUG)
嘗試獲取更多調試信息的另一件事-請嘗試以下操作,而不是print self.client.service.AddPerson(Person)
result = self.client.service.AddPerson(Person)
print str(result)
獲得異常的完整堆棧跟蹤也很有幫助-發生在哪一行以及調用堆棧是什么。 請嘗試注釋掉except Exception, e:
case except Exception, e:
處理,並在此處發布您將獲得的異常。
好,
看來我遇到的問題與Soap Header一代有關。 在SUDS中,有一個文件wsse.py,其中包含一個函數xml()。
def xml(self):
"""
Get xml representation of the object.
@return: The root node.
@rtype: L{Element}
"""
root = Element('UsernameToken', ns=wssens)
u = Element('Username', ns=wssens)
u.setText(self.username)
root.append(u)
p = Element('Password', ns=wssens)
p.set('Type', "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordDigest")
import sha
import base64
p.setText(base64.encodestring(sha.new(self.nonce + str(UTC(self.created)) + self.password).digest()).replace("\n", ''))
root.append(p)
if self.nonce is not None:
n = Element('Nonce', ns=wssens)
n.setText(base64.encodestring(self.nonce).replace("\n",''))
root.append(n)
if self.created is not None:
n = Element('Created', ns=wsuns)
n.setText(str(UTC(self.created)))
root.append(n)
return root
我在以下行中遇到錯誤:
p.setText(base64.encodestring(sha.new(self.nonce + str(UTC(self.created)) + self.password).digest()).replace("\n", ''))
因此,我注釋掉了這一行並添加了以下行:
p.setText(self.password)
請注意,我正在通過https撥打電話
問候
諾埃爾
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.