I have set up apache2 and mod_wsgi. I have a wsgi directory in which i have some python code saved in a *.wsgi file. The code is pasted below. In the web browser when i enter the url (localhost/wsgi/ape.wsgi) it displays the records returned from the database as a joined string.
What I am trying to do is deploy this as a webservice and be able to see its wsdl (localhost/wsgi/ape.wsgi/?wsdl). Is this possible? P.s. I am a newbie to Python. Thanks
import sys
sys.stdout = sys.stderr
import atexit
import threading
import cherrypy
from soaplib.wsgi_soap import SimpleWSGISoapApp
from soaplib.service import soapmethod
from soaplib.serializers.primitive import*
cherrypy.config.update({'environment': 'embedded'})
if cherrypy.__version__.startswith('3.0') and cherrypy.engine.state == 0:
cherrypy.engine.start(blocking=False)
atexit.register(cherrypy.engine.stop)
class Root(SimpleWSGISoapApp):
@soapmethod(_returns=String)
def index(self):
"""Get the data from the database and return list of rows"""
cursor = self._get_db_handle()
sql = """select name, source_comment from public.carpark where public_toilet = %s """ % 'TRUE'
results = []
cursor.execute(sql)
rows=cursor.fetchall()
for row in rows:
results.append(str(row))
joinedlist = ', '.join(results)
return joinedlist
cursor.close()
def _get_db_handle(self, host='xxxx.xxxx.com',
dbname='xxxx',user='xxxx',
password='xxxx',mapped=False):
"""Get the database handle"""
import psycopg2
from psycopg2 import extras
conn = psycopg2.connect("dbname='%s' user='%s' host='%s' password='%s'" % (dbname,user,host,password))
if not mapped:
db_handle = conn.cursor()
else:
db_handle = conn.cursor(cursor_factory=extras.DictCursor)
return db_handle
index.exposed = True
application = cherrypy.Application(Root(), script_name=None, config=None)