|« Python and MongoLab (Mongo in the Cloud)||Python classes to XML/XSD »|
We will develop the classic hello world!!! like a SOAP web service.
The purpose of this example is to explain how tornado-webservices works, with the use of the classic Hello World.
Note: For this example you must to use tornadows-0.9.3.2.
Step 1: Create a file named HelloService.py
1 import tornado.httpserver
2 import tornado.ioloop
3 from tornadows import soaphandler
4 from tornadows import webservices
5 from tornadows.soaphandler import webservice
7 class HelloWorldService(soaphandler.SoapHandler):
9 def sayHello(self):
10 return 'Hello World!!!'
12 if __name__ == '__main__':
13 service = [('HelloService',HelloWorldService)]
14 app = webservices.WebService(service)
15 ws = tornado.httpserver.HTTPServer(app)
First, the class HelloWorldService (lines 7 to 10) is the service that holds the operation or methods that contains the logical of the service.
With the decorator @webservice, indicates that the method sayHello () (lines 8 to 10) is the operation of the service.
In the line 13, service = [('HelloService','HelloWorldService')], indicates the name of the service that will be published on the net.
The tuple ('HelloWorld','HelloWorldService') associates the name of the service with the class of python. By this way the service can be accesed like:
Finally, in the line 16, indicates that we are listening from the port 8080.
Step 2: Running the service.
$ python HelloService.py
Step 3: Testing with a client.
Now, I will created a client to HelloService with suds (0.3.7).
Create a file named ClientHelloService.py with this code:
1 import suds
3 url = 'http://localhost:8080/HelloService?wsdl'
4 client = suds.client.Client(url,cache=None)
5 print client.service.sayHello()
Note that we don't pass any parameters to the operation sayHello().
line 1: we import the api python-suds.
line3: indicates the url with the Web Service Description Language (WSDL), in other words, the location of web service.
line 4: is created the client of the web service.
line 5: finally, is printed the result. The result should be (of course): "Hello World!!!"
For running the client, simply execute:
$ python ClientHelloWorld.py
You can see more examples in:
And documentation in:
You can download directly from:
For check prerequisites and install see: