使用 spyne 自定义标签

Custom tags with spyne

我正在尝试使用 twisted 和 spyne 设置小型 SOAP 1.1 服务器,但我似乎无法找到有关如何创建自定义标记(body)、名称空间或 headers 我的回复。有比创建我自己的 ProtocolBase 更好的方法吗?

目标是让 soap 响应看起来像这样:

<?xml version="1.0"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:cwmp="urn:dslforum-org:cwmp-1-2">
<SOAP-ENV:Header>
    <cwmp:ID SOAP-ENV:mustUnderstand="1">123456789</cwmp:ID>
</SOAP-ENV:Header>
<SOAP-ENV:Body>
    <cwmp:SetParameterValues>
        <ParameterList SOAP-ENC:arrayType="cwmp:ParameterValueStruct[1]">
            <ParameterValueStruct>
                <Name>MY.NAME</Name>
                <Value xsi:type="xsd:unsignedInt">4000</Value>
            </ParameterValueStruct>
        </ParameterList>
        <ParameterKey>Axess Update parameters</ParameterKey>
    </cwmp:SetParameterValues>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

我可以通过创建我自己的协议来生成我正在寻找的东西,我为此提供了一个类似于下面的 xml 字符串。

class spyne(ServiceBase):
    isLeaf = TwistedWebResource

    @rpc(AnyDict, AnyDict, _returns=Unicode)
    def my_rpc(ctx, DeviceId, ParameterList):

        out_document = etree.parse('data.xml')

        return etree.tostring(out_document, pretty_print=True,
                                         encoding='utf8', xml_declaration=False)

class my_protocol(XmlDocument):

    mime_type = 'text/xml; charset=utf-8'
    type = set(XmlDocument.type)
    type.update(('soap', 'soap11'))

    def __init__(self, *args, **kwargs):
        super(TR_069_SOAP, self).__init__(*args, **kwargs)

    def serialize(self, ctx, message):
        ctx.out_document = ctx.out_object[0]

    def create_out_string(self, ctx, charset=None):
       ctx.out_string = [ctx.out_document] 

我不确定是否有更好的方法来做到这一点。

Spyne 不支持使用 SOAP-ENC:arrayType="cwmp:ParameterValueStruct[1]" 样式序列化数组,无论如何。

您不需要自己的协议,但您确实需要覆盖 Soap11 中的 XmlDocument 的 complex_to_parent 并为数组添加特殊处理,例如, Array(某些类型,array_type="cwmp:ParameterValueStruct[1]")。

您可以修补 Spyne 本身并按照我的方式发送拉取请求,或者创建您自己的 Soap11 子类(不是 XmlDocument 子类)并在那里编写覆盖代码。

如果您想继续进行,请在 http://lists.spyne.io/listinfo/people 插话。