将 Soap XML 请求转换为 Zeep 的字典

Converting Soap XML request to dictionary for Zeep

我无法将此 XML 转换为相关的 python 字典 soap 请求以传递到 Zeep.Client.service。下面的XML来自:

https://psix.uscg.mil/XML/PSIXData.asmx?op=getVesselSummaryXMLString

    POST /XML/PSIXData.asmx HTTP/1.1
    Host: psix.uscg.mil
    Content-Type: application/soap+xml; charset=utf-8
    Content-Length: length

    <?xml version="1.0" encoding="utf-8"?>
    <soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
    xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
      <soap12:Body>
        <getVesselSummaryXMLString xmlns="http://cgmix.uscg.mil">
          <VesselID>string</VesselID>
          <VesselName>string</VesselName>
          <CallSign>string</CallSign>
          <VIN>string</VIN>
          <HIN>string</HIN>
          <Flag>string</Flag>
          <Service>string</Service>
          <BuildYear>string</BuildYear>
        </getVesselSummaryXMLString>
      </soap12:Body>
    </soap12:Envelope>

最终我想使用下面的代码向 soap 服务器发送一个请求,使用 python 字典作为 "request_data" 而不是上面的 XML,我只是不确定需要哪本词典。

url = 'https://cgmix.uscg.mil/xml/PSIXData.asmx?WSDL'
wsdl = url
client = zeep.Client(wsdl)
r = client.service.getVesselSummaryXMLString(request_data)

您可以使用以下方法检查 wsdl 方法:

python -mzeep https://cgmix.uscg.mil/xml/PSIXData.asmx?WSDL

从上面,我们可以看到 getVesselSummaryXMLString 方法只接受字符串参数:

getVesselSummaryXMLString(VesselID: xsd:string, VesselName: xsd:string, CallSign: xsd:string, VIN: xsd:string, HIN: xsd:string, Flag: xsd:string, Service: xsd:string, BuildYear: xsd:string) -> getVesselSummaryXMLStringResult: xsd:string

所以你可以像函数调用一样简单地调用它作为传递字符串参数:

r = client.service.getVesselSummaryXMLString('str1', 'str2', 'str3', 'str4', 'str5', 'str6', 'str7', 'str8')

如果要发送字典,则需要准备字典如下:

request_data = {'VesselID': 'str1', 'VesselName': 'string', 'CallSign': 'string', 'VIN': 'string', 'HIN': 'string', 'Flag': 'string', 'Service': 'string', 'BuildYear': 'str8'}
 r = client.service.getVesselSummaryXMLString(**request_data )

希望这能回答问题。