Python soap使用soaplib(服务器)和suds(客户端)

时间:2010-01-02 17:51:55

标签: python soap suds

这个问题与以下内容有关: Python SOAP server / client

对于使用python的soap,建议使用soaplib(http://wiki.github.com/jkp/soaplib)作为soap服务器,并使用suds(https://fedorahosted.org/suds/)作为soap客户端。 我的目标是在python中创建可由多个客户端(java等)使用的soap服务。 我尝试了soaplib(http://trac.optio.webfactional.com/wiki/HelloWorld)的HelloWorld示例。 当客户端也使用soaplib时,它运行良好。

然后,我尝试使用suds作为消费HelloWorld服务的客户端,但它失败了。 - 为什么会这样? soaplib服务器是否有不同客户端使用的问题?

这里是服务器的代码:

from soaplib.wsgi_soap import SimpleWSGISoapApp
from soaplib.service import soapmethod
from soaplib.serializers.primitive import String, Integer, Arraycode
class HelloWorldService(SimpleWSGISoapApp):
@soapmethod(String,Integer,_returns=Array(String))
def say_hello(self,name,times):
    results = []
    for i in range(0,times):
        results.append('Hello, %s'%name)
    return results

if __name__=='__main__':
from cherrypy.wsgiserver import CherryPyWSGIServer
#from cherrypy._cpwsgiserver import CherryPyWSGIServer
# this example uses CherryPy2.2, use cherrypy.wsgiserver.CherryPyWSGIServer for CherryPy 3.0
server = CherryPyWSGIServer(('localhost',7789),HelloWorldService())
server.start()

这是soaplib客户端:

from soaplib.client import make_service_client
from SoapServerTest_1 import HelloWorldService
client = make_service_client('http://localhost:7789/',HelloWorldService())
print client.say_hello("Dave",5)

结果:

>>> ['Hello, Dave', 'Hello, Dave', 'Hello, Dave', 'Hello, Dave', 'Hello, Dave']

这是suds客户端:

from suds.client import Client
url = 'http://localhost:7789/HelloWordService?wsdl'
client1 = Client(url)
client1.service.say_hello("Dave",5)

结果:

    >>> Unhandled exception while debugging...
Traceback (most recent call last):
  File "C:\Python25\Lib\site-packages\RTEP\Sequencing\SoapClientTest_1.py", line 10, in <module>
    client1.service.say_hello("Dave",5)
  File "c:\python25\lib\site-packages\suds\client.py", line 537, in __call__
    return client.invoke(args, kwargs)
  File "c:\python25\lib\site-packages\suds\client.py", line 597, in invoke
    result = self.send(msg)
  File "c:\python25\lib\site-packages\suds\client.py", line 626, in send
    result = self.succeeded(binding, reply.message)
  File "c:\python25\lib\site-packages\suds\client.py", line 658, in succeeded
    r, p = binding.get_reply(self.method, reply)
  File "c:\python25\lib\site-packages\suds\bindings\binding.py", line 158, in get_reply
    result = unmarshaller.process(nodes[0], resolved)
  File "c:\python25\lib\site-packages\suds\umx\typed.py", line 66, in process
    return Core.process(self, content)
  File "c:\python25\lib\site-packages\suds\umx\core.py", line 48, in process
    return self.append(content)
  File "c:\python25\lib\site-packages\suds\umx\core.py", line 63, in append
    self.append_children(content)
  File "c:\python25\lib\site-packages\suds\umx\core.py", line 140, in append_children
    cval = self.append(cont)
  File "c:\python25\lib\site-packages\suds\umx\core.py", line 61, in append
    self.start(content)
  File "c:\python25\lib\site-packages\suds\umx\typed.py", line 77, in start
    found = self.resolver.find(content.node)
  File "c:\python25\lib\site-packages\suds\resolver.py", line 341, in find
    frame = Frame(result, resolved=known, ancestry=ancestry)
  File "c:\python25\lib\site-packages\suds\resolver.py", line 473, in __init__
    resolved = type.resolve()
  File "c:\python25\lib\site-packages\suds\xsd\sxbasic.py", line 63, in resolve
    raise TypeNotFound(qref)
TypeNotFound: Type not found: '(string, HelloWorldService.HelloWorldService, )'

2 个答案:

答案 0 :(得分:2)

尝试将基元导入您的班级:

class HelloWorldService(SimpleWSGISoapApp):
    from soaplib.serializers.primitive import String, Integer, Arraycode
    @soapmethod(String,Integer,_returns=Array(String))

答案 1 :(得分:1)

如果您从主干获取最新来源,则会修复此错误,有关详细信息,请参阅https://github.com/soaplib/soaplib/pull/12