Skip to content

A service that provides the current temperature

rancavil edited this page Jan 17, 2012 · 1 revision

Now we will develop a SOAP web service that gives the current temperature.

The purpose of this examples is to demonstrate the use of methods of web services without input parameters.

Step 1: write the file TemperatureService.py.

   import tornado.httpserver
   import tornado.ioloop
   from tornadows import soaphandler
   from tornadows import webservices
   from tornadows import xmltypes
   from tornadows.soaphandler import webservice
   
   import random

   class TemperatureService(soaphandler.SoapHandler):
       """ Service that return the current temperature, not uses input parameters """
       @webservice(_params=None,_returns=int)
       def getCurrentTemperature(self):
           temperature = random.randint(0,40)
           return temperature

   if __name__ == '__main__':
       service = [('TemperatureService',TemperatureService)]
       app = webservices.WebService(service)
       ws  = tornado.httpserver.HTTPServer(app)
       ws.listen(8080)
       tornado.ioloop.IOLoop.instance().start()

The method of the web service generates a number between 0 and 40 that emulates the temperature. For this we used the random API of python.

Step 2: running the code.

   $ python TemperatureService.py

You can see the WSDL in the URL: http://localhost:8080/TemperatureService.py

Step 3: we will creating a client to the web service with python-suds (0.3.7).

Create a file named ClientTemp.py

   import suds
   
   url = 'localhost:8080/TemperatureService?wsdl'
   client = suds.client.Client(url,cache=None)
   temp = client.service.getCurrentTemperature()
   print 'The current temperature is : ',temp

Step 4: Execute the client.

   $ python ClientTemp.py

The results are:

The current temperature is : 34