简体   繁体   English

无法使用SOAPpy调用Web服务方法

[英]Can't call a webservice method using SOAPpy

I am trying to call a webservice using SOAPpy: 我正在尝试使用SOAPpy调用Web服务:

from SOAPpy import SOAPProxy

url = 'http://www.webservicex.net/WeatherForecast.asmx'

server = SOAPProxy(url);
print server.GetWeatherByPlaceName('Dallas');
print server.GetWeatherByZipCode ('33126');

The server call fails: 服务器调用失败:

Traceback (most recent call last):
  File "soap_test.py", line 6, in <module>
    print server.GetWeatherByPlaceName('Dallas');
  File "C:\usr\bin\Python26\lib\site-packages\SOAPpy\Client.py", line 451, in __call__
    return self.__r_call(*args, **kw)
  File "C:\usr\bin\Python26\lib\site-packages\SOAPpy\Client.py", line 473, in __r_call
    self.__hd, self.__ma)
  File "C:\usr\bin\Python26\lib\site-packages\SOAPpy\Client.py", line 387, in __call
    raise p
SOAPpy.Types.faultType: <Fault soap:Client: System.Web.Services.Protocols.SoapException: Server did not recognize the value of HTTP Header SOAPAction: GetWeatherByPlaceName.
   at System.Web.Services.Protocols.Soap11ServerProtocolHelper.RouteRequest()
   at System.Web.Services.Protocols.SoapServerProtocol.RouteRequest(SoapServerMessage message)
   at System.Web.Services.Protocols.SoapServerProtocol.Initialize()
   at System.Web.Services.Protocols.ServerProtocol.SetContext(Type type, HttpContext context, HttpRequest request, HttpResponse response)
   at System.Web.Services.Protocols.ServerProtocolFactory.Create(Type type, HttpContext context, HttpRequest request, HttpResponse response, Boolean& abortProcessing): >

What am I doing wrong? 我究竟做错了什么?

When consuming .NET webservices, you can add a soap action override to the call. 使用.NET Web服务时,可以将Soap Action覆盖添加到调用中。 Like the following. 像下面这样。 Confirmed working code. 确认的工作代码。

import SOAPpy

ns = 'http://www.webservicex.net'
url = '%s/WeatherForecast.asmx' % ns

server = SOAPpy.SOAPProxy( url, namespace=ns )
#following is required for .NET
server.config.buildWithNamespacePrefix = 0
#adding the soapaction is required for .NET
print server._sa( '%s/GetWeatherByPlaceName' %ns ).GetWeatherByPlaceName( PlaceName='Dallas' )
print server._sa( '%s/GetWeatherByZipCode' %ns ).GetWeatherByZipCode( ZipCode='33126' )

Someone wrote a class to do something similar. 有人写了一堂课来做类似的事情。

A modified version of the above wrapper for .Net: .Net的以上包装的修改后的版本:

import SOAPpy

class SOAPProxy( SOAPpy.SOAPProxy ):
    """Wrapper class for SOAPpy.SOAPProxy

    Designed so it will prepend the namespace to the action in the
    SOAPAction HTTP headers.
    """

    def __call( self, name, args, kw, ns=None, sa=None, hd=None, ma=None ):
        sa = sa or self.soapaction
        ns = ns or self.namespace
        self.config.buildWithNamespacePrefix = 0

        # Only prepend namespace if no soapaction was given.
        if ns and not sa:
            if ns.endswith( '/' ):
                sa = '%s%s' % ( ns , name )
            else:
                sa = '%s/%s' % ( ns , name )

        #fixup boolean args - .net wants lowercase
        for arg in kw:
            if isinstance( kw[ arg ], types.BooleanType ):
                kw[ arg ] =  SOAPpy.Types.booleanType( kw[ arg ] )


        return SOAPpy.SOAPProxy.__call( self, name, args, kw, ns, sa, hd, ma )

if __name__ == '__main__':
    print __doc__

As error message states, SOAPpy doesn't add SOAPAction HTTP header. 如错误消息所述,SOAPpy不会添加SOAPAction HTTP标头。 That's why SOAPpy won't work for many services. 这就是为什么SOAPpy无法用于许多服务的原因。 Try suds , here is a working example: 尝试suds ,这是一个有效的示例:

from suds.client import Client

url = 'http://www.webservicex.net/WeatherForecast.asmx?WSDL'
client = Client(url)

print client.service.GetWeatherByPlaceName('Dallas')
print client.service.GetWeatherByZipCode ('33126')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM