Python classes to XML/XSD

September 11th, 2011

If you want to test a python module that transforms python classes to XML and XSD documents look at:

https://github.com/rancavil/tornado-webservices/downloads/complextypes.tar.gz


Web Services and ComplexTypes

August 13th, 2011

It incorporates the mechanism for the use of python classes like xml complex types.

You must downloads and install the new release:

https://github.com/rancavil/tornado-webservices/downloads/tornadows-0.9.1.tar.gz

(Don't forget, you need have installed tornado web server).

In the example, the web service allows the consultation of products stored in a database.

Create the file ProductService.py

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

class Input(complextypes.ComplexType):
idProduct = complextypes.IntegerProperty()

class Product(complextypes.ComplexType):
id = complextypes.IntegerProperty()
name = complextypes.StringProperty()
price = complextypes.FloatProperty()
stock = complextypes.IntegerProperty()

class ProductService(soaphandler.SoapHandler):
@webservice(_params=Input,_returns=Product)
def getProduct(self, input):
id = input.idProduct.value

reg = self.database(id)

output = Product()
output.id.value = id
output.name.value = reg[0]
output.price.value = reg[1]
output.stock.value = reg[2]

return output

def database(self,id):
""" This method simulates a database of products """
db = {1:('COMPUTER',1000.5,100),
2:('MOUSE',10.0,300),
3:('PENCIL BLUE',0.50,500),
4:('PENCIL RED',0.50,600),
5:('PENCIL WHITE',0.50,900),
6:('HEADPHONES',15.7,500),
}
row = (None,0.0,0)
try:
row = db[id]
except:
None
return row

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

Executing the service with.

$ python ProductService.py

You can see the WSDL from http://localhost:8080/ProductService?wsdl

For testing the service, you can use SoapUI or use python-suds.

You can use the next code:

import suds

url = 'http://localhost:8080/ProductService?wsdl'
client = suds.client.Client(url)

print client
# Param IdProduct, you can test with 1,2,3...
idProduct = 1
output = client.factory.create('Product')
output = client.service.getProduct(idProduct)

print 'Result'
print '---------------------------------'
print 'Id.Product : %d'%output.id
print 'Name Product : %s'%output.name
print 'Price : %f'%output.price
print 'Stock : %d'%output.stock
print '---------------------------------'
print

You need have installed python-suds, you can download it from:

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

Now, you can running the script for test the service.

Web Services in Python

June 29th, 2011

At last, i finished an simple and useful SOAP Web Service API to work on Tornado Web Server. You can download it from Github repository https://github.com/rancavil/tornado-webservices, or directly from https://github.com/rancavil/tornado-webservices/downloads/tornadows-0.9.tar.gz

To install you must have Python 2.6+ and tornado 1.2.1+ (i have not tested with Python 3).

  1. Decompress the file tornadows-<version>.tar.gz
  2. Go into tornadows-<version> directory.
  3. Execute: python setup.py build
  4. Execute: python setup.py install

(I have tested it on Windows XP and Ubuntu, do not forget to use the sudo command on Ubuntu).

Now an simple first web service:

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

class SumService(soaphandler.SoapHandler):
@webservice(_params=[xmltypes.Integer,xmltypes.Integer],_returns=xmltypes.Integer)
def sum(self, a, b):
result = a + b
return result

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

Some features:

  1. This web service allows to sum of two integers.
  2. The WSDL is generated dynamically, you can get of http://localhost:8080/SumService?wsdl.
  3. I used soapUI tool to test this web service.
  4. In the file xmltypes.py, XML types tested are: Integer and String

Finally, i am working in the ToDO list.

 

Python

May 25th, 2011

Python has been a great programming language for developers, those who seek a flexible and easy mode to obtain fast and robust results. The reason is its simplicity and its big flexibility to extend the original features.

In the context of Internet, python has contributed with the development of a wide variety of frameworks and web servers,  today, i have been working a lot with tornado web server to develop the elements and components needed to develop and to publish SOAP web services.

My motivation is to develop a simple and practical manner for the design, development and implementation of web services, making use of the potentialities of tornado as web server. I hope to have soon a first usable version to publish in github.