使用Spyne soap服务器的合格元素/属性表单和不合格表单

时间:2013-05-07 13:14:19

标签: soap python-2.7 spyne

有没有办法在Spyne服务器上使用elementFormDefault =“nonqualified”服务器架构类型? 现在我的所有试验都以方法响应结果结束:

<senv:Envelope xmlns:tns="http://test.com/remoteService/"
xmlns:senv="http://schemas.xmlsoap.org/soap/envelope/">
<senv:Body>
    <tns:testResponse>
        <tns:status>ok</tns:status>
    </tns:testResponse>
</senv:Body>

使用“qualified”elementFormDefault生成wsdl片段:

<xs:schema targetNamespace="http://test.com/remoteService/" elementFormDefault="qualified"></xs:schema>

如何配置方法或参数模型以获得如下结果:

<senv:Envelope xmlns:tns="http://test.com/remoteService/"
xmlns:senv="http://schemas.xmlsoap.org/soap/envelope/">
<senv:Body>
    <tns:testResponse>
        <status>ok<status>
    </tns:testResponse>
</senv:Body>

我的目标是在子元素:

中生成结果
<tns:status>ok</tns:status>

将出现没有名称空间前缀 - 如下所示:

<status>ok<status>

2 个答案:

答案 0 :(得分:3)

如果您想知道如何将监听器添加到method_return_string的event_manager或其他事件,请参阅下面的完整示例:

from spyne import Application, rpc, ServiceBase, Iterable, Integer, Unicode

from spyne.protocol.soap import Soap11
from spyne.server.wsgi import WsgiApplication


class HelloWorldService(ServiceBase):
    @rpc(Unicode, Integer, _returns=Iterable(Unicode))
    def say_hello(ctx, name, times):
        for i in range(times):
            yield u'Hello, %s' % name


def on_method_return_string(ctx):
    ctx.out_string[0] = ctx.out_string[0].replace(b'Hello>', b'Good by')

HelloWorldService.event_manager.add_listener('method_return_string', 
                                              on_method_return_string)

application = Application([HelloWorldService], 'spyne.examples.hello.soap',
                          in_protocol=Soap11(validator='lxml'),
                          out_protocol=Soap11())

wsgi_application = WsgiApplication(application)


if __name__ == '__main__':
    import logging

    from wsgiref.simple_server import make_server
    server = make_server('127.0.0.1', 8000, wsgi_application)
    server.serve_forever()

从Spyne 2.12开始,这仍然是从响应变量中删除命名空间的唯一方法。

答案 1 :(得分:0)

截至2.10,Spyne不支持此事。

补丁有点毛茸茸。如果您愿意为此工作,请发送电子邮件至soap@python.org。

解决方法是从method_return_document挂钩中的传出文档中手动删除命名空间前缀。如果您还需要对传入文档强制执行相同的操作,则必须在document_built事件中修改Wsdl,或者使用软验证(软验证不关心命名空间)或根本不进行验证。