使用Python请求发送SOAP请求

使用Python请求发送SOAP请求,python,soap,python-requests,Python,Soap,Python Requests,可以使用Python库发送SOAP请求吗?确实可以 下面是一个使用普通请求库调用Weather 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

可以使用Python库发送SOAP请求吗?

确实可以

下面是一个使用普通请求库调用Weather 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
有些人提到了肥皂水图书馆。sud可能是与SOAP交互的更正确的方式,但我经常发现,当您有格式不正确的wdsl时,它会有一点恐慌(TBH,当您与仍然使用SOAP的机构打交道时,这更可能)

你可以用这样的肥皂水做上述操作:

from jinja2 import Environment, PackageLoader
env = Environment(loader=PackageLoader('myapp', 'templates'))
template = env.get_template('soaprequests/WeatherSericeRequest.xml')
body = template.render()
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 
注意:在使用肥皂水时,您几乎总是需要

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

sudo tcpdump -As 0 
这有助于检查实际通过连接的请求

上述两个代码片段也可以作为GIST提供:

from jinja2 import Environment, PackageLoader
env = Environment(loader=PackageLoader('myapp', 'templates'))
template = env.get_template('soaprequests/WeatherSericeRequest.xml')
body = template.render()
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 

这个解决方案怎么样?不应该使用肥皂水@为什么不呢?这是唯一适合该工作的工具。您可以将
请求。会话
传递给。@即使在2013年,suds已经失效。如果服务请求用户名和密码,该怎么办?在哪里注意它们?suds代码段现在给出“suds.transport.TransportError:HTTP错误500:Internal Server Error”。似乎服务器已关闭。@toast38coza-->在“/WeatherWS”应用程序中出现服务器错误。请更新示例。@toast38coza在上面的请求示例中,我从哪里获得头和主体变量的信息?fedorahosted.org上不再提供Suds文档。下面是Wayback机器上文档的快照。