【问题标题】:Making a SOAP request using Python requests module使用 Python requests 模块发出 SOAP 请求
【发布时间】:2013-03-12 05:59:51
【问题描述】:

我使用 python 请求模块来处理 REST 请求。

我正在尝试提出一个肥皂请求,但我想知道无法获得此示例。 这是我的肥皂正文和标题。

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

身体

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

这里是 wsdl 网址

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

请告诉我如何在此处使用 python 进行发布请求。如果无法使用 requests 模块,那么还有什么其他选择?

【问题讨论】:

  • 您是否尝试过专门的 SOAP 模块,例如 suds
  • 不,我打算使用休眠或请求或 httplib2
  • 那你需要手动构造请求并解析响应。
  • 哦,你能用我以前从未用过的泡沫给一些提示吗

标签: python soap


【解决方案1】:

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

这是我使用 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

真正重要的是在标头中指定 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 文件中不存在,则可以使用空字符串作为该标头的值

另见:

  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/

【讨论】:

  • 如果您需要发布到运行 WSDL 的 IIS,只需将 Content-Type 标头更改为 "Content-Type": "text/xml; charset=utf-8"。
  • 如果发布到 salesforce,请将 SOAPAction 值设置为“POST”以完成请求。
【解决方案2】:

要使用 SOAP 服务器,您应该使用专门的库。不幸的是,没有很好的 Python SOAP 客户端模块,我用过最好的一个是 suds

【讨论】:

    【解决方案3】:

    这在很大程度上是假设性的,因为我不知道有人实际实现了它,但 suds 支持对 suds.transport.Transport 进行自定义实现。一次是 suds.transport.http.HttpTransport。因此,从技术上讲,通过实现传输子类,您可以创建使用请求的 suds 传输。

    http://jortel.fedorapeople.org/suds/doc/suds.transport.http.HttpTransport-class.html 是默认使用的,它返回 http://jortel.fedorapeople.org/suds/doc/suds.transport.Reply-class.html 如您所见,包装请求回复应该相当简单,以便 suds 理解它们。此处记录了传输请求(应与请求一起发送) http://jortel.fedorapeople.org/suds/doc/suds.transport.Request-class.html 如果没有人喜欢我,我迟早会实现它

    为什么要这么努力?因为 suds 在内部使用 urllib2 远不如 python-requests 作为 HTTP 客户端实现。

    还有一个编辑:我提供了一个要点作为起点 https://gist.github.com/nanonyme/6268358 。它是 MIT 代码,未经测试,但应该作为传输的起点。

    【讨论】:

    • 现在有一个suds_requests 项目可以做到这一点。它为 suds(或 suds-jurko)库提供请求传输。
    【解决方案4】:

    万一有人最终读到了这篇文章:基于 suds 的解决方案大多停滞不前,但有一个名为 zeep 的更新的库,它写在 requests 和 lxml 之上。这是一个完全独立的解决方案,而不是像其他这样的泡沫叉。我从个人经验中知道,这在某些企业环境中使用。

    【讨论】:

      猜你喜欢
      • 2016-05-17
      • 2023-03-29
      • 2012-10-20
      • 1970-01-01
      • 2017-12-25
      • 1970-01-01
      • 1970-01-01
      • 2013-08-13
      • 2019-01-11
      相关资源
      最近更新 更多