Skip to main content
 首页 » 编程设计

python之使用 SUDS 将多个参数传递给 SOAP web 服务

2025年02月15日6JeffreyZhao

我想使用一个需要身份验证的 PHP SOAP 网络服务。然而,用户名和密码不需要在 HTTP header 中传递,而是作为 SOAP 参数传递。我只有一个运行查询的 PHP(工作)示例:

$soap_client->getCurrentOrderCustomers(array("user" => 'root', "password" => 'toor')); 

这工作正常并创建以下请求:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns1="https://example.com/webservice/interface_bi.php" xmlns:ns2="http://xml.apache.org/xml-soap" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"> 
   <SOAP-ENV:Body> 
      <ns1:getCurrentOrderCustomers> 
         <param0 xsi:type="ns2:Map"> 
            <item> 
               <key xsi:type="xsd:string">user</key> 
               <value xsi:type="xsd:string">root</value> 
            </item> 
            <item> 
               <key xsi:type="xsd:string">password</key> 
               <value xsi:type="xsd:string">toor</value> 
            </item> 
         </param0> 
      </ns1:getCurrentOrderCustomers> 
   </SOAP-ENV:Body> 
</SOAP-ENV:Envelope> 

假设客户端已正确创建(确实如此),我尝试在 Python 中执行以下操作:

result = ws_client.service.getCurrentOrderCustomers({'user':'root','password':'toor'}) 

还有这个:

result = ws_client.service.getCurrentOrderCustomers(user='root',password='toor') 

但是它们都导致以下请求,没有参数:

<SOAP-ENV:Envelope xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns0="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns1="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns2="https://example.com/webservice/interface_bi.php" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"> 
   <SOAP-ENV:Header/> 
   <ns1:Body> 
      <ns2:getCurrentOrderCustomers/> 
   </ns1:Body> 
</SOAP-ENV:Envelope> 

如果有人建议如何将参数传递给 webservice 函数以获得所需的请求,我将不胜感激。提前致谢。

请您参考如下方法:

我认为 - SOAP 服务 WSDL 告诉我们如何将参数传递给 SOAP 方法。 我尝试使用 suds 遵循以下代码 Python 和 SOAP 调用对我来说效果很好。

第 4 行 - 打印客户端显示所有带参数的 soap 方法

from suds.client import Client 
 
url="http://www.dneonline.com/calculator.asmx?wsdl" 
client = Client(url) 
 
print client ## shows the details of this service 
 
result = client.service.Multiply(10, 5) 
print result 

下面是上面脚本的输出

@ Pawel Sopel - 如果您分享您的 WSDL,我可以进一步调查。