python - 如何在 Python 中使用 zeep 格式化来自 WSDL 的 xml 请求
问题描述
我有一个 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() 得到了一个
意外的关键字参数“eID”。签名:`员工:
{ http://bos.example.com }员工
我需要的是如何使用 zeep 格式化上述请求。
解决方案
从包中导入parseString
参数xml
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>
推荐阅读
- java - 当前日期是前一周的日期
- css - css scroll snap 不适用于 React 应用程序中的 div
- sharepoint-2013 - SPField.FieldRenderingControl 等效于 CSOM 或 JSOM
- laravel - 编写自定义 Laravel 队列驱动程序
- javascript - 隐藏图表中的重复图例
- python - 如何修复python中不一致的return语句?
- tensorflow - deeplab 我的自定义数据集的权重标准是什么?
- java - 从 JAVA 中的 Jira 自定义字段中读取值
- javascript - 在本机 safari 应用程序扩展中,我们如何从 swift 调用带有参数的 javascript 函数?
- javascript - 用javascript突出显示字符串中的子字符串