如何用zeep生成SOAP请求

2024-06-17 15:22:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我在使用Python+Zeep生成SOAP请求时遇到了一个问题。我需要生成一个请求,比如这个示例SOAP请求:

   <soapenv:Header>
      <typ:UserCredentials>
         <typ:Username>username</typ:Username>
         <typ:Password>password</typ:Password>
         <!--Optional:-->
         <typ:Extension>
            <!--You may enter ANY elements at this point-->
         </typ:Extension>
         <!--You may enter ANY elements at this point-->
      </typ:UserCredentials>
   </soapenv:Header>
   <soapenv:Body>
      <typ:GetChildren>
         <typ:InterplayURI>uri</typ:InterplayURI>
         <!--Optional:-->
         <typ:IncludeFolders>true</typ:IncludeFolders>
         <!--Optional:-->
         <typ:IncludeFiles>false</typ:IncludeFiles>
         <!--Optional:-->
         <typ:IncludeMOBs>false</typ:IncludeMOBs>
         <!--Optional:-->
         <!--Optional:-->
         <typ:ReturnAttributes>
            <!--Zero or more repetitions:-->
            <typ:Attribute Group="?" Name="?">?</typ:Attribute>
         </typ:ReturnAttributes>
         <!--Optional:-->
         <typ:Extension>
            <!--You may enter ANY elements at this point-->
         </typ:Extension>
         <!--You may enter ANY elements at this point-->
      </typ:GetChildren>
   </soapenv:Body>
</soapenv:Envelope>

我用SOAPUI测试了这个请求,它工作得很好。在

使用Zeep,我无法产生类似的输出。在

我已经尝试过使用字典和传递元素本身来手动创建元素。我总是犯这样的错误:

^{pr2}$

Zeep生成的请求如下所示:

b'<soap-env:Envelope xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/"><soap-env:Header><ns0:Username xmlns:ns0="http://avid.com/interplay/ws/assets/types">user</ns0:Username><ns1:Password xmlns:ns1="http://avid.com/interplay/ws/assets/types">password</ns1:Password></soap-env:Header><soap-env:Body><ns0:GetChildren xmlns:ns0="http://avid.com/interplay/ws/assets/types"><ns0:InterplayURI xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns0:GetChildrenType"><ns0:InterplayURI>uri</ns0:InterplayURI></ns0:InterplayURI></ns0:GetChildren></soap-env:Body></soap-env:Envelope>'

服务器似乎无法理解此请求,因为它与开头显示的预期请求不同。在

我的python代码实际上是这样的。上面的输出没有被生成,但实际上已经被使用了。使用示例数据修改某些值:

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

wsdl = 'http://localhost/services/Assets?wsdl'
history = HistoryPlugin()
client = Client(wsdl, plugins=[history])
credentialType = client.get_type('ns0:UserCredentialsType')
credentials = credentialType(Username='user', Password='passwortd')
credentialTest = { 'UserCredentials': { 'Username':'user', 'Password':'password'}} 
param = client.get_type('ns0:GetChildrenType')
params = param(InterplayURI='uri') 
header = xsd.ComplexType([
    xsd.Sequence([
        xsd.Element('Username', xsd.String()),
        xsd.Element('Password', xsd.String())
        ])
    ])
header_value = header(Username='user', Password='password')
response = client.create_message(client.service, 'GetChildren', params, _soapheaders=[credentials])
request = etree.tostring(response)
serverResponse = client.service.GetChildren(request)
print(request)
print(serverResponse)

最后,使用Zeep生成这个输出似乎是一个模糊的过程,因为示例请求中有所有嵌套的自定义类型。我对Soap头中的凭证也有问题。在

有人知道如何做到这一点吗?也许自己编写这些xml更容易些?在


Tags: envclienthttpusernamepasswordoptionalsoapxsd