review packaging - isolate wsgi in a subdir
[plcapi.git] / apache / plc.wsgi
1 #
2 # Apache mod_wsgi python interface
3 #
4 # Copyright (C) 2004-2006 The Trustees of Princeton University
5 #
6
7 import sys
8 sys.path.append('/usr/share/plc_api')
9 sys.stdout = sys.stderr
10 import traceback
11 from PLC.Debug import log
12 from PLC.API import PLCAPI
13
14 api = PLCAPI()
15
16 def application(environ, start_response):
17     try:
18         status = '200 OK'
19         if environ.get('REQUEST_METHOD') != 'POST':
20             content_type = 'text/html'
21             output = """
22 <html><head>
23 <title>PLCAPI WSGI XML-RPC/SOAP Interface</title>
24 </head><body>
25 <h1>PLCAPI WSGI XML-RPC/SOAP Interface</h1>
26 <p>Please use XML-RPC or SOAP to access the PLCAPI.</p>
27 </body></html>
28 """
29         else:
30             api.environ = environ
31             content_type = 'text/xml'
32             ip = environ.get('REMOTE_ADDR')
33             port = environ.get('REMOTE_PORT')
34             output = api.handle((ip,port),  environ.get('wsgi.input').read())
35             # Shut down database connection, otherwise up to MaxClients DB
36             # connections will remain open.
37             api.db.close()
38     except Exception, err:
39         status = '500 Internal Server Error'
40         content_type = 'text/html'
41         output = 'Internal Server Error'
42         print >> log, err, traceback.format_exc()
43
44     # Write response
45     response_headers = [('Content-type', '%s' % content_type),
46                        ('Content-Length', str(len(output)))]
47     start_response(status, response_headers)
48     return [output] 
49