Spyne-如何从SOAP获取元素

时间:2014-05-30 07:34:39

标签: xml soap spyne

我第一次使用SPYNE。我想为TR069服务编写服务器。

我收到如下信息:

<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-0">
<soap-env:Header>
<cwmp:ID soap-env:mustUnderstand="1">984890494</cwmp:ID>
</soap-env:Header>
<soap-env:Body>
<cwmp:Inform>
<DeviceId>
<Manufacturer>xxxx</Manufacturer>
<OUI>xxxxxx</OUI>
<ProductClass>x-xxxx-xx</ProductClass>
<SerialNumber>xxxxxxxxxxxxx</SerialNumber>
</DeviceId>
<Event xsi:type="soap-enc:Array" soap-enc:arrayType="cwmp:EventStruct[1]">
<EventStruct>
<EventCode>2 PERIODIC</EventCode>
<CommandKey/>
</EventStruct>
</Event>
<MaxEnvelopes>1</MaxEnvelopes>
<CurrentTime>2014-05-21T10:47:37+08:00</CurrentTime>
<RetryCount>56</RetryCount>
<ParameterList xsi:type="soap-enc:Array" soap-enc:arrayType="cwmp:ParameterValueStruct[10]">
<ParameterValueStruct>
<Name>InternetGatewayDevice.DeviceSummary</Name>
<Value xsi:type="xsd:string"/>
</ParameterValueStruct>
<ParameterValueStruct>
<Name>InternetGatewayDevice.DeviceInfo.SpecVersion</Name>
<Value xsi:type="xsd:string">1.0</Value>
</ParameterValueStruct>
<ParameterValueStruct>
<Name>InternetGatewayDevice.DeviceInfo.HardwareVersion</Name>
<Value xsi:type="xsd:string">3FE55918ABAA</Value>
</ParameterValueStruct>
<ParameterValueStruct>
<Name>InternetGatewayDevice.DeviceInfo.SoftwareVersion</Name>
<Value xsi:type="xsd:string">3FE55935ADEA19</Value>
</ParameterValueStruct>
<ParameterValueStruct>
<Name>InternetGatewayDevice.DeviceInfo.ProvisioningCode</Name>
<Value xsi:type="xsd:string">123</Value>
</ParameterValueStruct>
<ParameterValueStruct>
<Name>InternetGatewayDevice.ManagementServer.ConnectionRequestURL</Name>
<Value xsi:type="xsd:string">http://x.x.x.x:7547/</Value>
</ParameterValueStruct>
<ParameterValueStruct>
<Name>InternetGatewayDevice.ManagementServer.ParameterKey</Name>
<Value xsi:type="xsd:string">3</Value>
</ParameterValueStruct>
<ParameterValueStruct>
<Name>InternetGatewayDevice.WANDevice.1.WANConnectionDevice.2.WANIPConnection.1.ExternalIPAddress</Name>
<Value xsi:type="xsd:string">10.18.92.182</Value>
</ParameterValueStruct>
<ParameterValueStruct>
<Name>InternetGatewayDevice.WANDevice.1.WANConnectionDevice.2.WANIPConnection.1.MACAddress</Name>
<Value xsi:type="xsd:string"/>
</ParameterValueStruct>
<ParameterValueStruct>
<Name>InternetGatewayDevice.WANDevice.1.WANConnectionDevice.1.WANPPPConnection.1.Username</Name>
<Value xsi:type="xsd:string">rock3</Value>
</ParameterValueStruct>
</ParameterList>
</cwmp:Inform>
</soap-env:Body>
</soap-env:Envelope>

我的代码如下:

class DeviceId(ComplexModel):
  __namespace__ = "urn:dslforum-org:cwmp-1-0"
  Manufacturer = String
  OUI          = String
  ProductClass = String
  SerialNumber = String

class EventStruct(ComplexModel):
  __namespace__ = "urn:dslforum-org:cwmp-1-0"
  EventCode   = String
  CommandKey  = String

class Event(ComplexModel):
  __namespace__ = "urn:dslforum-org:cwmp-1-0"
  EventStruct = EventStruct

class ParameterValueStruct(ComplexModel):
  __namespace__ = "urn:dslforum-org:cwmp-1-0"
  Name  = String
  Value = String

class ParameterList(ComplexModel):
  __namespace__ = "urn:dslforum-org:cwmp-1-0"
  ParameterValueStruct = ParameterValueStruct

class CWMP(ServiceBase):
  @srpc(DeviceId, Event, Integer, String, Integer, Array(ParameterList), _returns=Iterable(Unicode))
  def Inform(DeviceId, Event, MaxEnvelopes, CurrentTime, RetryCount, ParameterList):
    pList = []
    for i in ParameterList:
      print i.ParameterValueStruct
      pList.append(i.ParameterValueStruct.Name)
    print DeviceId
    print Event
    print Event.EventStruct.EventCode
    print MaxEnvelopes
    print CurrentTime
    ...

除ParameterList

外,正常获取所有变量
(Pdb) type(i)
<class '__main__.ParameterList'>
(Pdb) type(i.ParameterValueStruct)
<type 'NoneType'>
(Pdb) print i
ParameterList()
(Pdb) print i.ParameterValueStruct
None

有没有人知道如何在ParameterValueStruct中获取所有名称/值?

1 个答案:

答案 0 :(得分:1)

这是我修复的内容:

  1. 当您手动定义包装器对象时,您不需要Array()
  2. Spyne不支持soap:enc attrs。
  3. 如果要使用lxml验证,则需要<cwmp:Inform>的子标记的正确名称空间。
  4. 如果要控制元素的顺序,最好使用_type_info表示法
  5. 代码:

    class DeviceId(ComplexModel):
      __namespace__ = "urn:dslforum-org:cwmp-1-0"
      _type_info = [
        ('Manufacturer', String),
        ('OUI',          String),
        ('ProductClass', String),
        ('SerialNumber', String),
      ]
    
    class EventStruct(ComplexModel):
      __namespace__ = "urn:dslforum-org:cwmp-1-0"
      EventCode   = String
      CommandKey  = String
    
    class Event(ComplexModel):
      __namespace__ = "urn:dslforum-org:cwmp-1-0"
      EventStruct = EventStruct.customize(max_occurs='unbounded')
    
    class ParameterValueStruct(ComplexModel):
      __namespace__ = "urn:dslforum-org:cwmp-1-0"
      Name  = String
      Value = String
    
    class ParameterList(ComplexModel):
      __namespace__ = "urn:dslforum-org:cwmp-1-0"
      ParameterValueStruct = ParameterValueStruct.customize(max_occurs='unbounded')
    
    class CWMP(ServiceBase):
      @srpc(DeviceId, Event, Integer, String, Integer, ParameterList, _returns=Iterable(Unicode))
      def Inform(DeviceId, Event, MaxEnvelopes, CurrentTime, RetryCount, ParameterList):
        pList = []
        for i in ParameterList.ParameterValueStruct:
          print i
          pList.append(i.Name)
        print DeviceId
        print Event
        print Event.EventStruct[0].EventCode
        print MaxEnvelopes
        print CurrentTime
    
    
    application = Application([CWMP], 'urn:dslforum-org:cwmp-1-0',
                in_protocol=Soap11(validator='lxml'),
                out_protocol=Soap11()
            )
    

    请求:

    <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-0">
        <soap-env:Header>
            <cwmp:ID soap-env:mustUnderstand="1">984890494</cwmp:ID>
        </soap-env:Header>
        <soap-env:Body>
            <cwmp:Inform xmlns="urn:dslforum-org:cwmp-1-0">
                <DeviceId>
                    <Manufacturer>xxxx</Manufacturer>
                    <OUI>xxxxxx</OUI>
                    <ProductClass>x-xxxx-xx</ProductClass>
                    <SerialNumber>xxxxxxxxxxxxx</SerialNumber>
                </DeviceId>
                <Event>
                    <EventStruct>
                        <EventCode>2 PERIODIC</EventCode>
                        <CommandKey/>
                    </EventStruct>
                </Event>
                <MaxEnvelopes>1</MaxEnvelopes>
                <CurrentTime>2014-05-21T10:47:37+08:00</CurrentTime>
                <RetryCount>56</RetryCount>
                <ParameterList>
                    <ParameterValueStruct>
                        <Name>InternetGatewayDevice.DeviceSummary</Name>
                        <Value xsi:type="xsd:string"/>
                    </ParameterValueStruct>
                    <ParameterValueStruct>
                        <Name>InternetGatewayDevice.DeviceInfo.SpecVersion</Name>
                        <Value xsi:type="xsd:string">1.0</Value>
                    </ParameterValueStruct>
                    <ParameterValueStruct>
                        <Name>InternetGatewayDevice.DeviceInfo.HardwareVersion
                        </Name>
                        <Value xsi:type="xsd:string">3FE55918ABAA</Value>
                    </ParameterValueStruct>
                    <ParameterValueStruct>
                        <Name>InternetGatewayDevice.DeviceInfo.SoftwareVersion
                        </Name>
                        <Value xsi:type="xsd:string">3FE55935ADEA19</Value>
                    </ParameterValueStruct>
                    <ParameterValueStruct>
                        <Name>InternetGatewayDevice.DeviceInfo.ProvisioningCode
                        </Name>
                        <Value xsi:type="xsd:string">123</Value>
                    </ParameterValueStruct>
                    <ParameterValueStruct>
                        <Name>
                            InternetGatewayDevice.ManagementServer.ConnectionRequestURL
                        </Name>
                        <Value xsi:type="xsd:string">http://x.x.x.x:7547/</Value>
                    </ParameterValueStruct>
                    <ParameterValueStruct>
                        <Name>InternetGatewayDevice.ManagementServer.ParameterKey
                        </Name>
                        <Value xsi:type="xsd:string">3</Value>
                    </ParameterValueStruct>
                    <ParameterValueStruct>
                        <Name>
                            InternetGatewayDevice.WANDevice.1.WANConnectionDevice.2.WANIPConnection.1.ExternalIPAddress
                        </Name>
                        <Value xsi:type="xsd:string">10.18.92.182</Value>
                    </ParameterValueStruct>
                    <ParameterValueStruct>
                        <Name>
                            InternetGatewayDevice.WANDevice.1.WANConnectionDevice.2.WANIPConnection.1.MACAddress
                        </Name>
                        <Value xsi:type="xsd:string"/>
                    </ParameterValueStruct>
                    <ParameterValueStruct>
                        <Name>
                            InternetGatewayDevice.WANDevice.1.WANConnectionDevice.1.WANPPPConnection.1.Username
                        </Name>
                        <Value xsi:type="xsd:string">rock3</Value>
                    </ParameterValueStruct>
                </ParameterList>
            </cwmp:Inform>
        </soap-env:Body>
    </soap-env:Envelope>