0

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)
  • So what is the actual problem? You don't really say. Have you read the CherryPy documentation on how to deploy under mod_wsgi? – Graham Dumpleton Aug 30 '12 at 22:55
  • I don't really understand how to configure it properly. I want to be able to call the webservice using the a URL similar to the following: http://localhost/wsgi/webservice.wsgi/?wsdl. I tried adding the following line to bottom of script: application = cherrypy.Application(Root(), script_name=None, config=None). But I can't open the page. Forgive my ignorance but i'm learning python. – user1636352 Aug 31 '12 at 09:11
  • Amend your question with the mod_wsgi configuration you used, where you placed your WSGI script file and the actual error message you are getting in the browser and/or Apache error log file. – Graham Dumpleton Aug 31 '12 at 10:14
  • I've updated the code in the posting. When I enter the url in my browser (http://localhost/wsgi/ape.wsgi) it displays a string of the records return from database. I want to expose this as a webservice. How do i go about doing this? – user1636352 Aug 31 '12 at 10:35
  • So is not a deployment problem now and a coding issue with your application. Can't help you with that. – Graham Dumpleton Sep 01 '12 at 03:48
  • I'm finding it hard to find some documentation on deploying a python webservice with apache. Any pointers? – user1636352 Sep 04 '12 at 08:39
  • Did you at least find http://code.google.com/p/modwsgi/wiki/IntegrationWithCherryPy and follow the links on that? – Graham Dumpleton Sep 05 '12 at 00:57

0 Answers0