Setting tag plcapi-5.4-2
[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.Logger import logger
13 from PLC.API import PLCAPI
14
15 def application(environ, start_response):
16     try:
17         status = '200 OK'
18         if environ.get('REQUEST_METHOD') != 'POST':
19             content_type = 'text/html'
20             output = """
21 <html><head>
22 <title>PLCAPI WSGI XML-RPC/SOAP Interface</title>
23 </head><body>
24 <h1>PLCAPI WSGI XML-RPC/SOAP Interface</h1>
25 <p>Please use XML-RPC or SOAP to access the PLCAPI.</p>
26 </body></html>
27 """
28         else:
29             # Thomas Dreibholz <dreibh@simula.no>
30             # Note that this function is called within multiple threads!
31             # "api" MUST be a local variable instead of a global one.
32             # Otherwise, this causes concurrent accesses to the same
33             # object within different threads!
34             api = PLCAPI()
35             api.environ = environ
36             content_type = 'text/xml'
37             ip = environ.get('REMOTE_ADDR')
38             port = environ.get('REMOTE_PORT')
39             output = api.handle((ip,port),  environ.get('wsgi.input').read())
40             # Shut down database connection, otherwise up to MaxClients DB
41             # connections will remain open.
42             api.db.close()
43     except Exception as err:
44         status = '500 Internal Server Error'
45         content_type = 'text/html'
46         output = 'Internal Server Error'
47         logger.exception("INTERNAL ERROR !!")
48
49     # Write response
50     response_headers = [('Content-type', '%s' % content_type),
51                        ('Content-Length', str(len(output)))]
52     start_response(status, response_headers)
53     return [output] 
54