[英]Python Suds Soap Client Error
I am trying to connect to a web service using Python/SUDS. 我正在尝试使用Python / SUDS连接到Web服务。
I have the following code in a single file and I am able to connect successfully and I receive a response. 我在单个文件中包含以下代码,并且能够成功连接并且收到响应。
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:'
I want to use this code in a Python client app that will be called from a button click event. 我想在将从按钮单击事件中调用的Python客户端应用程序中使用此代码。 I have tried to implement this and I am able to print out the client but when I make the web service call ( print self.client.service.AddPerson(Person)
) I get the following error. 我已经尝试实现此功能,并且能够打印出客户端,但是当我进行Web服务调用( print self.client.service.AddPerson(Person)
)时,出现以下错误。
unsupported operand type(s) for +: 'NoneType' and 'str'
How do I go about fixing this error? 我该如何解决该错误?
Looks like webservice call returns None. 看起来Web服务调用返回None。 Additional information is required to identify what went wrong. 需要其他信息来确定出了什么问题。
First of all - enable suds logging by adding before calling Suds_Connect. 首先-通过在调用Suds_Connect之前添加来启用suds日志记录。 This should provide you information about what's going on in suds under the hood. 这应该为您提供有关引擎盖下肥皂水发生情况的信息。
import logging
logging.basicConfig(level=logging.DEBUG)
Another thing to try for getting more debug information - please try the following instead of print self.client.service.AddPerson(Person)
尝试获取更多调试信息的另一件事-请尝试以下操作,而不是print self.client.service.AddPerson(Person)
result = self.client.service.AddPerson(Person)
print str(result)
Also it would be helpful to get full stack trace for exception - which line it happens at and what is the call stack. 获得异常的完整堆栈跟踪也很有帮助-发生在哪一行以及调用堆栈是什么。 Please try to comment out exception handling for except Exception, e:
case and post here the exception you'll get. 请尝试注释掉except Exception, e:
case except Exception, e:
处理,并在此处发布您将获得的异常。
Ok, 好,
It seems the issue I was having relates to the Soap Header generation. 看来我遇到的问题与Soap Header一代有关。 In SUDS there is a file wsse.py that contains a function xml(). 在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
I was getting an error @ the following line: 我在以下行中遇到错误:
p.setText(base64.encodestring(sha.new(self.nonce + str(UTC(self.created)) + self.password).digest()).replace("\n", ''))
So I commented out this line and added the following line: 因此,我注释掉了这一行并添加了以下行:
p.setText(self.password)
Note that I am making a call over https 请注意,我正在通过https拨打电话
regards 问候
Noel 诺埃尔
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.