python zeep嵌套响应不是python数据结构

时间:2017-01-19 06:15:29

标签: python-2.7 soap lxml zeep

我使用python 2.7和zeep作为SOAP Client来检索一些信息。我注意到client.service.ServiceName的响应返回一个python数据结构,e.i一个列表,但它的项不是python数据结构,而是lxml.etree._Element。你能否告诉我是否有办法让zeep返回嵌套的python词典而不是xml。

以下是代码:

# python 2.7
import zeep
from lxml import etree
print zeep.__version__    # '0.23.0'
history = zeep.plugins.HistoryPlugin()
client = zeep.Client(wsdl=wsdl, 
                     wsse=UsernameToken(username, password), 
                     plugins=[history])
projectId, cardUniqueId, cardType, userLoginId = ('3', '111', 'XXX', 'myuser')
cards = client.service.getCard(projectId, cardUniqueId, cardType, userLoginId)
print(type(cards))   # <type 'list'>
print(cards)         # [<Element {http://kanbancard.webservices.kanban.app.digite.com/}fields at 0x7f706d69e640>]
print(type(cards[0]))    # <type 'lxml.etree._Element'>

client.service.getCard的回复是:

print(etree.tounicode(history.last_received['envelope'], pretty_print=True))

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
  <soapenv:Body>
    <ns:getCardResponse xmlns:ns="http://kanbancard.webservices.kanban.app.digite.com/">
      <ns:fields>
        <ns:field ns:name="cardNumber">XYZ</ns:field>
        <ns:field ns:name="name">foobar</ns:field>
        <ns:field ns:name="description">foobar</ns:field>
        <ns:field ns:name="priority">High</ns:field>
        <ns:field ns:name="classOfService">Standard Class</ns:field>
        <ns:field ns:name="cardSize">1</ns:field>
        <ns:field ns:name="dateIdentified">2017-01-19T04:07:53Z</ns:field>
        <ns:field ns:name="ExternalCardId">XYZ</ns:field>
        <ns:field ns:name="currentSwimId">1632088</ns:field>
        <ns:field ns:name="currentQueueId">2322634</ns:field>
      </ns:fields>
    </ns:getCardResponse>
  </soapenv:Body>
</soapenv:Envelope>

WSLD:

<wsdl:operation name="getCard">
  <wsdl:input message="axis2:getCardRequestMessage">
</wsdl:input>
  <wsdl:output message="axis2:getCardResponseMessage">
</wsdl:output>
</wsdl:operation>
        <xs:element name="getCardRequest">
            <xs:complexType>
                <xs:sequence>
                    <xs:element name="projectId" nillable="false" type="xs:string"/>
                    <xs:element name="cardUniqueId" nillable="false" type="xs:string"/>
                    <xs:element name="cardType" nillable="false" type="xs:string"/>
                    <xs:element name="userLoginId" nillable="true" type="xs:string"/>
                </xs:sequence>
            </xs:complexType>
        </xs:element>
        <xs:element name="getCardResponse">
            <xs:complexType>
                <xs:sequence>
                    <xs:any maxOccurs="unbounded" minOccurs="0"/>
                </xs:sequence>
            </xs:complexType>
        </xs:element>
  <wsdl:message name="getCardResponseMessage">
    <wsdl:part name="part1" element="axis2:getCardResponse">
  </wsdl:part>
  <wsdl:message name="getCardRequestMessage">
    <wsdl:part name="part1" element="axis2:getCardRequest">
    </wsdl:part>
  </wsdl:message>
  <wsdl:message name="getCardResponseMessage">
    <wsdl:part name="part1" element="axis2:getCardResponse">
    </wsdl:part>
  </wsdl:message>

1 个答案:

答案 0 :(得分:0)

简而言之,zeep不理解这个模式,所以它只返回xml

最好的方法是使用历史记录

https://github.com/mvantellingen/python-zeep/issues/311