Custom tags with spyne

时间:2016-07-11 20:34:00

标签: soap twisted spyne

Im trying to set up a small SOAP 1.1 server with twisted and spyne, but I can't seem to find anything on how to create custom tags(body), namespaces, or headers for my responses. Is there a better way to do this than creating my own ProtocolBase?

The goal is to have soap responses that look like this:

<?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>

Im able to produce what I'm looking for by creating my own protocol for which I hand a string of xml similar to below.

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] 

I'm not sure if there is a better way to be doing this.

1 个答案:

答案 0 :(得分:0)

Spyne不支持使用SOAP-ENC:arrayType="cwmp:ParameterValueStruct[1]"样式序列化数组,无论这意味着什么。

您不需要自己的协议,但是您需要在XmlDocument中覆盖Soap11的complex_to_parent,并为具有Array(SomeType)的数组添加特殊处理,array_type =&#34; cwmp:ParameterValueStruct [1]&#34;)。

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

如果您希望以任何方式继续,请在http://lists.spyne.io/listinfo/people处加注。