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