使用 Python 请求发送 SOAP 请求

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18175489/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 10:04:09  来源:igfitidea点击:

Sending SOAP request using Python Requests

pythonsoappython-requests

提问by Deepankar Bajpeyi

Is it possible to use Python's requestslibrary to send a SOAP request?

是否可以使用 Python 的requests库来发送 SOAP 请求?

采纳答案by toast38coza

It is indeed possible.

确实有可能。

Here is an example calling the Weather SOAP Service using plain requests lib:

下面是一个使用普通请求库调用 Wea​​ther SOAP 服务的示例:

import requests
url="http://wsf.cdyne.com/WeatherWS/Weather.asmx?WSDL"
#headers = {'content-type': 'application/soap+xml'}
headers = {'content-type': 'text/xml'}
body = """<?xml version="1.0" encoding="UTF-8"?>
         <SOAP-ENV:Envelope xmlns:ns0="http://ws.cdyne.com/WeatherWS/" xmlns:ns1="http://schemas.xmlsoap.org/soap/envelope/" 
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
            <SOAP-ENV:Header/>
              <ns1:Body><ns0:GetWeatherInformation/></ns1:Body>
         </SOAP-ENV:Envelope>"""

response = requests.post(url,data=body,headers=headers)
print response.content

Some notes:

一些注意事项:

  • The headers are important. Most SOAP requests will not work without the correct headers. application/soap+xmlis probably the more correctheader to use (but the weatherservice prefers text/xml
  • This will return the response as a string of xml - you would then need to parse that xml.
  • For simplicity I have included the request as plain text. But best practise would be to store this as a template, then you can load it using jinja2 (for example) - and also pass in variables.
  • 标题很重要。如果没有正确的标头,大多数 SOAP 请求将无法工作。application/soap+xml可能是更正确的标题使用(但天气服务更喜欢text/xml
  • 这会将响应作为 xml 字符串返回 - 然后您需要解析该 xml。
  • 为简单起见,我将请求包含为纯文本。但最佳实践是将其存储为模板,然后您可以使用 jinja2(例如)加载它 - 并传入变量。

For example:

例如:

from jinja2 import Environment, PackageLoader
env = Environment(loader=PackageLoader('myapp', 'templates'))
template = env.get_template('soaprequests/WeatherSericeRequest.xml')
body = template.render()

Some people have mentioned the suds library. Suds is probably the more correctway to be interacting with SOAP, but I often find that it panics a little when you have WDSLs that are badly formed (which, TBH, is more likely than not when you're dealing with an institution that still uses SOAP ;) ).

有些人提到了 suds 库。Suds 可能是与 SOAP 交互的更正确的方式,但我经常发现,当您的 WDSL 格式不正确时,它会有点恐慌(TBH,当您与一个仍然存在使用 SOAP ;) )。

You can do the above with suds like so:

你可以像这样用肥皂水做上面的事情:

from suds.client import Client
url="http://wsf.cdyne.com/WeatherWS/Weather.asmx?WSDL"
client = Client(url)
print client ## shows the details of this service

result = client.service.GetWeatherInformation() 
print result 

Note:when using suds, you will almost always end up needing to use the doctor!

注意:使用肥皂水时,您几乎总是需要使用医生

Finally, a little bonus for debugging SOAP; TCPdump is your friend. On Mac, you can run TCPdump like so:

最后,调试 SOAP 的一点好处;TCPdump 是您的朋友。在 Mac 上,你可以像这样运行 TCPdump:

sudo tcpdump -As 0 

This can be helpful for inspecting the requests that actually go over the wire.

这有助于检查实际通过线路的请求。

The above two code snippets are also available as gists:

以上两个代码片段也可作为要点提供: