【问题标题】:Can't call a webservice method using SOAPpy无法使用 SOAPpy 调用 Web 服务方法
【发布时间】:2009-11-20 03:35:42
【问题描述】:

我正在尝试使用 SOAPpy 调用网络服务:

from SOAPpy import SOAPProxy

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

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

服务器调用失败:

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): >

我做错了什么?

【问题讨论】:

    标签: python soappy web-services soap .net


    【解决方案1】:

    使用 .NET Web 服务时,您可以在调用中添加一个肥皂操作覆盖。像下面这样。确认的工作代码。

    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' )
    

    有人写了class 来做类似的事情。

    上述 .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__
    

    【讨论】:

      【解决方案2】:

      正如错误消息所述,SOAPpy 不添加 SOAPAction HTTP 标头。这就是为什么 SOAPpy 不能用于许多服务的原因。试试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')
      

      【讨论】:

      • 我会检查泡沫。谢谢。
      • 其中一个网络服务需要经过身份验证才能使用它。我将如何使用 suds 对其进行身份验证?任何帮助请
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-04-10
      • 1970-01-01
      • 2015-03-17
      • 1970-01-01
      • 1970-01-01
      • 2013-04-01
      • 1970-01-01
      相关资源
      最近更新 更多