python - 使用 Python 请求模块发出 SOAP 请求

标签 python soap

我对 REST 请求使用了 python 请求模块。

我正在尝试提出 soap 请求,但我想知道无法为此获取示例。 这是我的 SOAP 主体和标题。

<auth>
<apikey>xcvzxcvcxzv-a0-0035c6fbc04f</apikey>
</auth>

正文

<reports>
<report>
<name>Test</name>
</report>
</reports>

这里是wsdl url

https://ltn.net/webservices/booking/r1/index.wsdl

请告诉我如何使用 python 在此处发出发布请求。如果无法使用 requests 模块,那么其他替代方案是什么?

最佳答案

我最近遇到了同样的问题,不幸的是,suds 和 jurko-suds(维护的 suds fork )都无法提供帮助。 这主要是因为 suds 不断生成格式错误的 soap 信封(如果应该生成的 soap 包含一些应该在 CDATA 中的内容,这种情况尤其会发生)与服务器所期望的不同。即使我尝试使用 __inject 选项自己注入(inject) SOAP 信封也是如此。

这是我使用 python 请求解决它的方法

import requests

request = u"""<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:req="http://SOME_URL">
   <soapenv:Header>
         <user>{0}</user>
         <pass>{1}</pass>
   </soapenv:Header>
   <soapenv:Body>
      <req:RequestInfo><![CDATA[<?xml version='1.0' encoding='UTF-8'?><request xmlns='http://SOME_OTHER_URL'>
    <Call>
        <callID>{2}</callID>
        ...
    </Call>
    <Foo>
        <Bar>
            <Baz>{3}</Baz>
            ...
        </Bar>
        <Neytri>
            ...
        </Neytri>
    </Foo>
    <Title>{4}</Title>
</request>]]></req:RequestInfo>
   </soapenv:Body>
</soapenv:Envelope>""".format('Jake_Sully', 
                              'super_secret_pandora_password',
                              'TYSGW-Wwhw',
                              'something_cool',
                              'SalaryPayment',
                              'Pandora_title',
                            )

encoded_request = request.encode('utf-8')

headers = {"Host": "http://SOME_URL",
            "Content-Type": "application/soap+xml; charset=UTF-8",
            "Content-Length": str(len(encoded_request)),
            "SOAPAction": "http://SOME_OTHER_URL"}

response = requests.post(url="http://SOME_OTHER_URL",
                     headers = headers,
                     data = encoded_request,
                     verify=False)

print response.content #print response.text

真正重要的是在 header 中指定 Content-Type 以及 SOAPAction。 根据SOAP 1.1 specifiaction

 The SOAPAction HTTP request header field can be used to indicate the intent of the SOAP HTTP request. The value is a URI identifying the intent. SOAP places no restrictions on the format or specificity of the URI or that it is resolvable. An HTTP client MUST use this header field when issuing a SOAP HTTP Request.       

SOAPAction 的值通常可以在您要进行的 API 调用的 wsdl 文件中找到;如果 wsdl 文件中不存在,那么您可以使用空字符串作为该 header 的值

另见:

  1. http://archive.oreilly.com/pub/post/unraveling_the_mystery_of_soap.html
  2. http://www.prodigyproductionsllc.com/articles/programming/call-a-webservice-using-soap-and-python/

关于python - 使用 Python 请求模块发出 SOAP 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15569330/

相关文章:

python - 函数内的 Tensorflow variable_scope : tf. 占位符和 tf.get_variable

python - 如何从数据框列中提取城市(格式不同)

java - 将 Soap XML 响应转换为对象

Haskell:使用哪个 SOAP 客户端库?

python - 加速自定义聚合函数

python - 如何在 Kivy 中正确导入 MapSource?

Python 语言/语法用法

php - SoapFault 异常 [HTTP] 无法连接到主机 : With Local SSL Certificate

PHP SoapServer - 节点中的属性

java - 在JAVA中创建SOAP消息请求