Python读取wsdl不起作用

时间:2012-12-11 22:06:50

标签: python wsdl

我正在尝试使用此代码从wsdl获取数据。

查询网站的zipid(“60630”)工作正常,但在我的代码中它给出了错误

“无效的ZIP”

wsdlFile = 'http://wsf.cdyne.com/WeatherWS/Weather.asmx?wsdl'
wsdlObject = WSDL.Proxy(wsdlFile)
wsdlObject.show_methods()
zipid = "60630"
result = wsdlObject.GetCityWeatherByZIP(ZIP=zipid)
print result[1]

有人可以帮忙解决这里错误以及为什么代码无法正常工作。

谢谢!!!

1 个答案:

答案 0 :(得分:0)

问题可能是您的客户端发送服务器不理解的请求。似乎你正在使用SOAPpy,这是我在尝试时发送的请求:

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope
  SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
  xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
  xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance"
  xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
  xmlns:xsd="http://www.w3.org/1999/XMLSchema"
>
<SOAP-ENV:Body>
<GetCityWeatherByZIP SOAP-ENC:root="1">
<v1 xsi:type="xsd:string">60630</v1>
</GetCityWeatherByZIP>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

使用suds时的含义:

from suds.client import Client
cli = Client("http://wsf.cdyne.com/WeatherWS/Weather.asmx?wsdl")
cli.service.GetCityWeatherByZIP(ZIP=60630)

它产生:

<?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:GetCityWeatherByZIP>
      <ns0:ZIP>60630</ns0:ZIP>
    </ns0:GetCityWeatherByZIP>
  </ns1:Body>
</SOAP-ENV:Envelope>

(使用wireshark捕获)

第二个请求从服务器返回有效结果。

我对SOAPpy的了解不足以提出解决方法,但也许您可以考虑将客户端库切换为suds。