2009-11-20 5 views
1

Я пытаюсь вызвать веб-сервиса с помощью SOAPpy:Невозможно вызвать метод WebService с помощью 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): > 

Что я делаю неправильно?

ответ

4

Как указано в сообщении об ошибке, SOAPpy не добавляет HTTP-заголовок SOAPAction. Вот почему 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') 
+0

Я проверю мыльную пену. Спасибо. – artdanil

+0

Пена сделала то, что хотела. – artdanil

+0

Один из веб-сервисов должен быть аутентифицирован, прежде чем я смогу его использовать. Как я могу аутентифицировать его? любая помощь plz – deeshank

7

При потреблении .NET WebServices, вы можете добавить переопределение мыло действий для вызова. Как и следующее. Подтвержденный рабочий код.

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__ 
Смежные вопросы