Suds es un tremendo cliente SOAP para consumir web services es basado en Python a diferencia de los clientes en Java no hay generación de clases, y el API es tipo orientado a objetos. Y lo mejor es el poco código que se debe escribir para consumir un servicio web y para muestra un ejemplo sencillo:
Lo primero instalar el cliente ya sea descargándolo del sitio o instalando el paquetes si nuestra distribución de linux lo incluye en sus repositorios.
# fedora 15 yum -y install python-suds # tarball wget https://fedorahosted.org/releases/s/u/suds/python-suds-0.4.tar.gz tar xzvf python-suds-0.4.tar.gz cd python-suds-0.4 python setup.py install
Para el ejemplo utilizaremos uno de los webservices del sitio webservicesx.net
#!/usr/bin/env python from suds.client import Client url = 'http://www.webservicex.net/globalweather.asmx?WSDL' client = Client(url) print client
si todo esta correcto la salida del script mostrara esto:
web service output
Suds ( https://fedorahosted.org/suds/ ) version: 0.3.9 GA build: R659-20100219
Service ( GlobalWeather ) tns="http://www.webserviceX.NET"
Prefixes (0)
Ports (2):
(GlobalWeatherSoap)
Methods (2):
GetCitiesByCountry(xs:string CountryName, )
GetWeather(xs:string CityName, xs:string CountryName, )
Types (0):
(GlobalWeatherSoap12)
Methods (2):
GetCitiesByCountry(xs:string CountryName, )
GetWeather(xs:string CityName, xs:string CountryName, )
Types (0):
El servicio web tiene 2 metodos los cuales podemos invocar de la siguiente manera:
#!/usr/bin/env python
from suds.client import Client
url = 'http://www.webservicex.net/globalweather.asmx?WSDL'
client = Client(url)
#print client
weather = client.service.GetWeather('Managua', 'Nicaragua')
print weather
esto generara un string conteniendo el xml para que luego pueda ser procesado según se requiera .
<?xml version="1.0" encoding="utf-16"?> <CurrentWeather> <Location>Managua A. C. Sandino, Nicaragua (MNMG) 12-09N 086-10W 56M</Location> <Time>Jul 18, 2011 - 12:00 AM EDT / 2011.07.18 0400 UTC</Time> <Wind> from the ESE (110 degrees) at 5 MPH (4 KT):0</Wind> <Visibility> greater than 7 mile(s):0</Visibility> <SkyConditions> partly cloudy</SkyConditions> <Temperature> 77 F (25 C)</Temperature> <DewPoint> 73 F (23 C)</DewPoint> <RelativeHumidity> 88%</RelativeHumidity> <Pressure> 29.88 in. Hg (1011 hPa)</Pressure> <Status>Success</Status> </CurrentWeather>
Referencias:
Python Suds
1 Response to Consumir web services SOAP facil con Python Suds
Consumir un servicio web desde Python « Microbuffer
May 28th, 2012 at 3:54 pm
[...] http://www.rz0r.net/2011/07/consumir-web-services-soap-facil-con-python-suds/ Valora esto:Comparte esto:MásMe gusta:Me gustaSé el primero en decir que te gusta esta [...]