使用apache2部署soap web服务

时间:2012-08-30 14:59:46

标签: python web-services apache mod-wsgi soaplib

我已经设置了apache2和mod_wsgi。我有一个wsgi目录,其中我有一些* .wsgi文件中保存的python代码。代码粘贴在下面。在Web浏览器中,当我输入url(localhost / wsgi / ape.wsgi)时,它会将数据库返回的记录显示为连接字符串。

我想要做的是将其部署为web服务,并能够看到它的wsdl(localhost / wsgi / ape.wsgi /?wsdl)。这可能吗?附:我是Python的新手。感谢

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)

0 个答案:

没有答案