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