Skip to content

The Classic Hello World!!!

rancavil edited this page Jan 3, 2012 · 6 revisions

We will develop the classic hello world!!! like a SOAP web service.

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

Note 1: If you wanna learn how to install the tornado-webservice, you check the Prerequisites and Intalling tornado-webservices sections (steps 1 and 2) into the wiki:

https://github.com/rancavil/tornado-webservices/wiki/Python-Web-Services-with-Tornado

The code of the web service Hello World is:

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

   class HelloWorldService(soaphandler.SoapHandler):
         """ Service that return Hello World!!!, not uses input parameters """
         @webservice(_params=None,_returns=xmltypes.String)
         def sayHello(self):
             return "Hello World!!!"
   
   if __name__ == '__main__':
       service = [('HelloWorldService',HelloWorldService)]
       app = webservices.WebService(service)
       ws  = tornado.httpserver.HTTPServer(app)
       ws.listen(8080)
       tornado.ioloop.IOLoop.instance().start()

You can save the code in a file named HelloWorldService.py.

To running the code, you must execute:

   $ python HelloWorldService.py

To access at the WSDL of the web service go to: http://localhost:8080/HelloWorldService?wsdl

Note 2: If you want test the web service from other machine on the net, you must replace localhost with the IP address of the machine that execute the service.

The operation sayHello() of the web service return a string with "Hello, World!!!". The operation not require input parameters.

Now we will testing the web service:

Step 1: We will creating a client to the web service with python-suds.

I use the python-suds-0.3.7 for the examples:

https://fedorahosted.org/releases/s/u/suds/python-suds-0.3.7.tar.gz

Create a file named ClientHelloWorld.py

   import suds
   
   url = 'http://localhost:8080/HelloWorldService?wsdl'
   client = suds.client.Client(url,cache=None)
   # Here, are display the services characteristics
   print client
   # The sayHello() method responds
   print '**The service say : ',client.service.sayHello(),'**'

Step 2: Running the code.

   $ python ClientHelloWorld.py

Step 3: See the results.

   Suds ( https://fedorahosted.org/suds/ )  version: 0.3.7 GA  build: R580-20091016
   
   Service ( HelloWorldService ) tns="http://localhost:8080/HelloWorldService/sayHello"
      Prefixes (1)
         ns0 = "http://localhost:8080/HelloWorldService/sayHello"
      Ports (1):
         (HelloWorldServicePort)
            Methods (1):
               sayHello()
            Types (1):
               paramsTypes

   **The service say :  Hello World!!!**

The response of the web service is Hello World!!!.