python的服务端点接口

时间:2014-05-28 06:49:15

标签: java web-services python-3.x service endpoint

我正在研究java,其中eclipse提供了一些工具,如wsimport,它通过指定工具的Web服务的URL来导入所有java文件和类文件。有没有像python这样的东西?我们如何使用python来使用任何Web服务。 python应该有一些服务端点接口吗?如果有,我们如何使用它?请提前帮助,谢谢。

1 个答案:

答案 0 :(得分:0)

Jurko's port of suds将完成这项工作。它易于使用,它公开了Web服务的方法和对象类型。

示例:

>>> import suds
>>> from suds.client import Client
>>> client = Client('http://wsf.cdyne.com/WeatherWS/Weather.asmx?WSDL')
>>> print(client)
Service ( Weather ) tns="http://ws.cdyne.com/WeatherWS/"
   Prefixes (1)
       ns0 = "http://ws.cdyne.com/WeatherWS/"
   Ports (2):
       (WeatherSoap)
          Methods (3):
             GetCityForecastByZIP(xs:string ZIP)
             GetCityWeatherByZIP(xs:string ZIP)
             GetWeatherInformation()
          Types (8):
             ArrayOfForecast
             ArrayOfWeatherDescription
             Forecast
             ForecastReturn
             POP
             WeatherDescription
             WeatherReturn
             temp
...

如您所见,Client类只需要一个URL即可打印出Web服务的信息。

>>> weather = client.service.GetCityWeatherByZIP('02118')
>>> print(weather)
(WeatherReturn){
   Success = True
   ResponseText = "City Found"
   State = "MA"
   City = "Boston"
   WeatherStationCity = "Boston"
   WeatherID = 14
   Description = "Cloudy"
   Temperature = "64"
   RelativeHumidity = "80"
   Wind = "S9"
   Pressure = "30.19F"
   Visibility = None
   WindChill = None
   Remarks = None
 }

client对象具有service属性,可让您运行方法。当我运行GetCityWeatherByZIP时,suds将XML响应转换为Python对象(在本例中为WeatherReturn对象)。您可以像访问任何其他Python对象一样访问其属性。

>>> weather.Description
Cloudy
>>> weather.Wind
S9

您还可以使用Client.factory创建自己的XML关联对象。

>>> forecast = client.factory.create('Forecast')
>>> forecast.WeatherID = 6
>>> print(forecast)
(Forecast){
   Date = None
   WeatherID = 6
   Desciption = None
   Temperatures = 
      (temp){
         MorningLow = None
         DaytimeHigh = None
      }
   ProbabilityOfPrecipiation = 
      (POP){
         Nighttime = None
         Daytime = None
      }
 }

虽然有点过时,this doc page应该可以帮助您入门。