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