gpt4 book ai didi

python - 如何在 Python 中使用 zeep 格式化来自 WSDL 的 xml 请求

转载 作者:太空宇宙 更新时间:2023-11-03 20:46:18 25 4
gpt4 key购买 nike

我有一个 xml 请求的示例,应该像这样发送到 SOAP 服务器:

<soapenv:Envelope 
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:ser="http://service.example.com"
xmlns:bos="http://bos.example.com"
xmlns:ser1="http://service.example.com">
<soapenv:Header/>
<soapenv:Body>
<ser:UploadRequest ac="YY">
<ser:updateRecord>
<ser:employee ptc="xxx" lastname="Example" firstname="Employee"
gender="F">
<bos:employment eID="testEmployee" doj="2000-01-17"/>
<bos:employment-status startDate="2000-01-17" status="active"/>
</ser:employee>
</ser:updateRecord>
</ser:UploadRequest>
</soapenv:Body>
</soapenv:Envelope>

这里的服务名称是Upload,操作是updateRecord。我无法使用 python zeep 格式化此请求。

我已经使用soapui进行了测试,它有效。现在我需要使用 python 和 zeep 发送请求,但我仍然失败,因为我是 SOAP 和 Zeep 的新手。

这是我尝试过的:

from requests import Session
from zeep import Client
from zeep.cache import SqliteCache
from zeep.transports import Transport
from lxml import etree

session = Session()
session.cert = 'client.pem'
transport = Transport(session=session,cache=SqliteCache())
client = Client('example.wsdl',transport=transport)


request_data = {
'updateRecord':{
'ac': 'HF',
'ptc': 'yy',
'lastname':'Lasme',
'firstname':'Didier',
'gender':'M',
'eID':'ACI001014',
'doj':'2000-01-17'
}
}

xml = client.create_message(client.service,'Upload',**request_data)
print(etree.tostring(xml, encoding="unicode", pretty_print=True))

我收到此错误

TypeError: {http://service.example.com}UpdateRecord() got an
unexpected keyword argument 'eID'. Signature: `employee:
{http://bos.example.com}Employee

我需要的是如何使用zeep来格式化上述请求。

最佳答案

xml包中导入parseString参数

from xml.dom.minidom import parseString

然后按照下面的代码操作

result = parseString(xml).toprettyxml() # xml is your variable
print(result)

结果:

<?xml version="1.0" ?>
<employees>
<employee>
<Name>Leonardo DiCaprio</Name>
</employee>
</employees>

关于python - 如何在 Python 中使用 zeep 格式化来自 WSDL 的 xml 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56578400/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com