如何使用zeep正确生成xml?

时间:2019-05-21 21:52:41

标签: python web-services soap zeep

我正在尝试生成一个XML以使用ZEEP消耗Web服务,但是结果xml对于该服务无效,元素中的类型未正确添加。如何正确添加元素中的xsi类型:type =“ ns1:Array” / xsi:type =“ ns0:string”?或正确配置zeep以创建具有所有属性的元素?。

这是发送的内容:

<SOAP-ENV:Envelope xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns0="http://www.w3.org/2001/XMLSchema" xmlns:ns1="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns2="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns3="http://sandbox.coordinadora.com/agw/ws/guias/1.6/server.php" xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <SOAP-ENV:Body>
    <ns3:Guias_imprimirRotulos>
      <p>
        <id_rotulo>55</id_rotulo>
        <codigos_remisiones/>
        <usuario>myuser.ws</usuario>
        <clave>6fb1bf6de4550985c</clave>
      </p>
    </ns3:Guias_imprimirRotulos>
  </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

这是我的代码:

import zeep
from zeep import Client
from zeep.plugins import HistoryPlugin
from lxml import etree

history = HistoryPlugin()
wsdl = 'http://sandbox.coordinadora.com/agw/ws/guias/1.6/server.php?wsdl'
client = Client(wsdl=wsdl, plugins=[history])
client.set_ns_prefix('SOAP-ENC', 'http://schemas.xmlsoap.org/soap/encoding/')
client.set_ns_prefix('SOAP-ENV', 'http://schemas.xmlsoap.org/soap/envelope/')
client.set_ns_prefix('xsi', 'http://www.w3.org/2001/XMLSchema-instance')
client.set_ns_prefix('ns0', 'http://www.w3.org/2001/XMLSchema')
client.set_ns_prefix('ns1', 'http://schemas.xmlsoap.org/soap/encoding/')
client.set_ns_prefix('ns2', 'http://schemas.xmlsoap.org/soap/envelope/')
client.set_ns_prefix('ns3', 'http://sandbox.coordinadora.com/agw/ws/guias/1.6/server.php')
client.set_ns_prefix('encodingStyle', 'http://schemas.xmlsoap.org/soap/encoding/')


data = {'id_rotulo':'55',
    'codigos_remisiones':['68630005830'],
    'usuario':'myuser.ws',
    'clave':'6fb1bf6de4550985c'
    }

client.service.Guias_imprimirRotulos(data)

for hist in [history.last_sent, history.last_received]:
    print(etree.tostring(hist["envelope"], encoding="unicode", pretty_print=True))

这应该发送:

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns0="http://www.w3.org/2001/XMLSchema" xmlns:ns1="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns3="http://sandbox.coordinadora.com/agw/ws/guias/1.6/server.php" xmlns:ns2="http://schemas.xmlsoap.org/soap/envelope/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
    <SOAP-ENV:Header/>
    <ns2:Body>
        <ns3:Guias_imprimirRotulos>
            <p xsi:type="ns3:Agw_imprimirRotulosIn">
                <id_rotulo xsi:type="ns0:string">55</id_rotulo>
                <codigos_remisiones xsi:type="ns1:Array">
                    <id_remision xsi:type="ns0:string">68630005829</id_remision>
                </codigos_remisiones>
                <usuario xsi:type="ns0:string">myuser.ws</usuario>
                <clave xsi:type="ns0:string">6fb1bf6dc37090a7e4550985c</clave>
            </p>
        </ns3:Guias_imprimirRotulos>
    </ns2:Body>
</SOAP-ENV:Envelope>

1 个答案:

答案 0 :(得分:0)

wsdl = "http://sandbox.coordinadora.com/agw/ws/guias/1.6/server.php?wsdl"
client = zeep.Client(wsdl=wsdl)

str_element = client.get_element("ns1:string")
str_tracking_code = zeep.xsd.AnyObject(str_element, "68630005830")

response = client.service.Guias_imprimirRotulos(
    p={
        "id_rotulo": "44",
        "codigos_remisiones": [str_tracking_code],
        "usuario": user,
        "clave": hashlib.sha256(
            bytes(password, encoding="utf-8")
        ).hexdigest(),
    }
)