如何在python中测试SOAP服务?

时间:2019-03-13 19:22:32

标签: python python-3.x soap zeep spyne

在Python中,我使用SPYNE libray创建了一个soap服务。代码如下:

import logging
logging.basicConfig(level=logging.DEBUG)
from spyne import Application, rpc, ServiceBase, Unicode
from spyne.protocol.xml import XmlDocument
from spyne.protocol.soap import Soap11
from spyne.server.wsgi import WsgiApplication


class notification(ServiceBase):
    @rpc(Unicode, _returns=Unicode)
    def notificacion(ctx, mensaje):
        return 'Hello, %s' % mensaje


application = Application([notification],
    tns='notificacion',
    in_protocol=XmlDocument(validator='lxml'),
    out_protocol=Soap11()
)
if __name__ == '__main__':
    from wsgiref.simple_server import make_server
    wsgi_app = WsgiApplication(application)
    server = make_server('0.0.0.0', 8080, wsgi_app)
    server.serve_forever()

现在我需要尝试该服务。我创建了一个使用以下服务的ZEEP库客户端:

import zeep

var ='<?xml version="1.0" encoding="UTF-8"?><ShowAccesslists><Access-List><Type>Extended IP</Type><Name>test</Name></Access-List></ShowAccesslists>'

wsdl = 'http://0.0.0.0:8080'
client = zeep.Client(wsdl=wsdl)
print(client.service.notificacion(var))

但是我不确定如何测试调用您创建的服务的结果。有人可以指导我吗? 我找不到如何使客户步行的方法。服务器已经接我了,但是当我执行客户端的代码时,它使我感到震惊。我还不知道如何证明它或如何调用。

除此之外,还会引发以下错误:

407客户端错误:URL http://0.0.0.0:8080/ 需要认证,

1 个答案:

答案 0 :(得分:-1)

NullServer是很好的测试功能。但是为了完成客户端测试,我建议从HttpClient来的spyne.client.httpClient来的suds.client的情况下开始这个例子。

简单易用:

from spyne.client.http import HttpClient

c = HttpClient('http://localhost:8000/', application)

u = c.factory.create("User")

u.user_name = 'dave'
u.first_name = 'david'

retval = c.service.add_user(u)

from suds.client import Client

c = Client('http://localhost:8000?wsdl')
u = c.factory.create("User")

u.user_name = 'dave'
u.full_name = 'david smith'

retval = c.service.put_user(u)

参考号:spyne_client