How to build webservice on Python framework.
I tried turbogear framework for building webservice. It is simple and straightforward. It provides "WSDL", "SOAP call", and "RESTful" function. Before trying this, need to go through turbogear framework to understand the tg's MVC model approach. Here are some notes.
1. Required library:
turbogears framework :
http://www.turbogears.org/
Turbogears webService(TGWebService) :
http://code.google.com/p/tgws/
http://pypi.python.org/pypi/TGWebServices
2. As like building TG MVC program, just type “tg-admin quickstart” to create a new project
3. Modify controller.py to "Import tg lib and tgWS lib"
import turbogears as tg from turbogears import controllers, expose, flash
from tgwebservices.controllers import WebServicesRoot, \
wsexpose,wsvalidate
4. Create a method for calling
class MyWSController (WebServicesRoot):
@wsexpose(int)
@wsvalidate(int,int)
#@wsexpose('json') //→ Adding a Json output format
def multi(self, a, b):
return a*b
5. Bind calling method into ROOT
class Root(controllers.RootController):
myservices = MyWSController ('http://xxxxx.dyndns.org:8000/myservices/')
6. Then, Start up, just type “python start-XXX.py”
7. TgWS framework provides WSDL and RESTFul service.
RESTful : http://192.168.0.126:8000/myservices/multi?a=1&b=4
WSDL: http://192.168.0.126:8000/myservices/soap/api.wsdl
8. Whole codes for controller.py
Code:
import turbogears as tg
from turbogears import controllers, expose, flash
# from webservice import model
# import logging
# log = logging.getLogger("webservice.controllers")
from tgwebservices.controllers import WebServicesRoot, \
wsexpose,wsvalidate
class MyWSController (WebServicesRoot):
@wsexpose(int)
@wsvalidate(int,int)
#@wsexpose('json')
def multi(self, a, b):
return a*b
class Root(controllers.RootController):
myservices = MyWSController ('http://xxxxx.dyndns.org:8000/myservices/')
#cherrpy.root = MyWSController ('http://192.168.0.126:8000')
1. Required library:
turbogears framework :
http://www.turbogears.org/
Turbogears webService(TGWebService) :
http://code.google.com/p/tgws/
http://pypi.python.org/pypi/TGWebServices
2. As like building TG MVC program, just type “tg-admin quickstart” to create a new project
3. Modify controller.py to "Import tg lib and tgWS lib"
import turbogears as tg from turbogears import controllers, expose, flash
from tgwebservices.controllers import WebServicesRoot, \
wsexpose,wsvalidate
4. Create a method for calling
class MyWSController (WebServicesRoot):
@wsexpose(int)
@wsvalidate(int,int)
#@wsexpose('json') //→ Adding a Json output format
def multi(self, a, b):
return a*b
5. Bind calling method into ROOT
class Root(controllers.RootController):
myservices = MyWSController ('http://xxxxx.dyndns.org:8000/myservices/')
6. Then, Start up, just type “python start-XXX.py”
7. TgWS framework provides WSDL and RESTFul service.
RESTful : http://192.168.0.126:8000/myservices/multi?a=1&b=4
WSDL: http://192.168.0.126:8000/myservices/soap/api.wsdl
8. Whole codes for controller.py
Code:
import turbogears as tg
from turbogears import controllers, expose, flash
# from webservice import model
# import logging
# log = logging.getLogger("webservice.controllers")
from tgwebservices.controllers import WebServicesRoot, \
wsexpose,wsvalidate
class MyWSController (WebServicesRoot):
@wsexpose(int)
@wsvalidate(int,int)
#@wsexpose('json')
def multi(self, a, b):
return a*b
class Root(controllers.RootController):
myservices = MyWSController ('http://xxxxx.dyndns.org:8000/myservices/')
#cherrpy.root = MyWSController ('http://192.168.0.126:8000')