使用 Python 请求发送 SOAP 请求

编程入门 行业动态 更新时间:2024-10-27 10:26:19
本文介绍了使用 Python 请求发送 SOAP 请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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

推荐答案

确实有可能.

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

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

import requests url="wsf.cdyne/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="ws.cdyne/WeatherWS/" xmlns:ns1="schemas.xmlsoap/soap/envelope/" xmlns:xsi="www.w3/2001/XMLSchema-instance" xmlns:SOAP-ENV="schemas.xmlsoap/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

一些注意事项:

  • 标题很重要.如果没有正确的标头,大多数 SOAP 请求将无法工作.application/soap+xml 可能是更正确 使用的标头(但是weatherservice 更喜欢text/xml
  • 这会将响应作为 xml 字符串返回 - 然后您需要解析该 xml.
  • 为简单起见,我将请求包含为纯文本.但最佳实践是将其存储为模板,然后您可以使用 jinja2(例如)加载它 - 并传入变量.
  • The headers are important. Most SOAP requests will not work without the correct headers. application/soap+xml is probably the more correct header 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.

例如:

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

有些人提到了 suds 库.Suds 可能是与 SOAP 交互更正确的方式,但是我经常发现当您的 WDSL 格式错误时它会有点恐慌(TBH,当您重新与仍在使用 SOAP 的机构打交道 ;) ).

Some people have mentioned the suds library. Suds is probably the more correct way 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 ;) ).

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

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

注意:在使用 suds 时,您几乎总是需要看医生!

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

最后,调试 SOAP 的一点小收获;TCPdump 是您的朋友.在 Mac 上,你可以像这样运行 TCPdump:

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

sudo tcpdump -As 0

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

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

以上两个代码片段也可作为要点使用:

  • 带有请求的 SOAP 请求
  • 带有 suds 的 SOAP 请求

更多推荐

使用 Python 请求发送 SOAP 请求

本文发布于:2023-11-26 06:09:03,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1632942.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Python   SOAP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!