【问题标题】:Service endpoint interface for pythonpython的服务端点接口
【发布时间】:2014-05-28 06:49:14
【问题描述】:

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

【问题讨论】:

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


    【解决方案1】:

    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 应该可以帮助您入门。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-10-16
      • 2017-03-31
      • 2022-10-04
      • 2020-10-22
      • 2016-05-31
      • 1970-01-01
      • 2019-01-03
      相关资源
      最近更新 更多