Python 如何使用zeep正确生成xml?

Python 如何使用zeep正确生成xml?,python,web-services,soap,zeep,Python,Web Services,Soap,Zeep,我试图生成一个xml来使用ZEEP使用web服务,但生成的xml对服务无效,元素中的类型添加不正确。如何在元素中正确添加xsi-types:type=“ns1:Array”/xsi:type=“ns0:string”?或者正确配置zeep以创建具有所有属性的元素 这是发送的内容: 这是应该发送的内容: 55 68630005829 myuser.ws 6fb1bf6dc37090a7e4550985c 虽然此代码可以回答问题,但提供关于如何和/或为什么解决问题的附加上下文将提高答案的长期价

我试图生成一个xml来使用ZEEP使用web服务,但生成的xml对服务无效,元素中的类型添加不正确。如何在元素中正确添加xsi-types:type=“ns1:Array”/xsi:type=“ns0:string”?或者正确配置zeep以创建具有所有属性的元素

这是发送的内容: 这是应该发送的内容:

55 68630005829 myuser.ws 6fb1bf6dc37090a7e4550985c


虽然此代码可以回答问题,但提供关于如何和/或为什么解决问题的附加上下文将提高答案的长期价值。虽然此代码可以回答问题,但提供关于如何和/或为什么解决问题的附加上下文将提高答案的长期价值。
<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>
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(),
    }
)