-
Notifications
You must be signed in to change notification settings - Fork 32
Python Web Services with Tornado
This is an API for the development of soap web services using the python programming language and tornado web server. The goals of this api, is provide an simple and easy way of develop and deploy soap web service.
You need to have installed:
- python (version 2.6 or later, it's not has tested with python 3) (http://www.python.org/download/).
- tornado web server 2.1.1 (https://github.com/facebook/tornado/downloads/tornado-2.1.1.tar.gz).
Download the last version from https://github.com/rancavil/tornado-webservices/downloads
In a directory decompress the file (.zip or tar.gz).
On Linux:
$ tar zxvf tornadows-x.y.z.tar.gz
$ cd tornadows-x.y.z/
$ python setup.py build
$ sudo python setup.py install
On Windows:
Decompress the zip file with WinRAR, WinZip or similar.
C:\> cd tornadows-x.y.z
C:\tornadows-x.y.z\> python setup.py build
C:\tornadows-x.y.z\> python setup.py install
For test the demos and examples, I recommend use of SoapUI and python-suds.
SoapUI, can be download from http://www.soapui.org or http://www.eviware.com/Download-soapUI/download-older-versions.html (i am using version 3.5.1).
python-suds, can be download from https://fedorahosted.org/releases/s/u/suds/python-suds-0.3.7.tar.gz.
A first example: We will created a service that generate and return a xml document that represent a birth certificate.
Create a file CertService.py
Write the next code:
import tornado.ioloop
from tornadows import soaphandler, webservices, complextypes
from tornadows.soaphandler import webservice
import datetime
""" This class represent a xml document with the request of birth certificate """
class InputRequest(complextypes.ComplexType):
idperson = str
""" This class represent the birth certificate (the xml document with the response) """
class CertificateResponse(complextypes.ComplexType):
numcert = int
idperson = str
nameperson = str
birthday = datetime.date
datetimecert = datetime.datetime
isvalid = bool
""" This class is the web service that return the birth certificate """
class CertService(soaphandler.SoapHandler):
@webservice(_params=InputRequest, _returns=CertificateResponse)
def getCertificate(self, input):
idperson = input.idperson
cert = CertificateResponse()
cert.numcert = 1
cert.idperson = idperson
cert.nameperson = 'Steve J'
cert.birthday = datetime.date(1973,12,11)
cert.datetimecert = datetime.datetime.now()
cert.isvalid = True
return cert
""" Here, we will deployed the web service through tornado web server """
if __name__ == '__main__':
service = [('CertService',CertService)]
app = webservices.WebService(service)
app.listen(8080)
tornado.ioloop.IOLoop.instance().start()
As you can see, I use the data types available in python (int, str, datetime module) on the attributes of the classes, this allow defined the xml complex types into the xml schema of the WSDL document.
Run and test the web service.
Running on Linux:
$ python CertService.py
Running on Windows:
c:\> python CertService.py
To test that web service is running:
In a web browser put the URL: http://localhost:8080/CertService?wsdl
You should see the WSDL document that defines the web service.
Testing the service:
To test the web service, I recommend using soapUI. Because you can to appreciate how the web service work at low level.