Merge remote-tracking branch 'origin/pycurl' into planetlab-4_0-branch
[plcapi.git] / ModPython.py
1 #
2 # Apache mod_python interface
3 #
4 # Aaron Klingaman <alk@absarokasoft.com>
5 # Mark Huang <mlhuang@cs.princeton.edu>
6 #
7 # Copyright (C) 2004-2006 The Trustees of Princeton University
8 # $Id: ModPython.py,v 1.5 2007/02/12 18:42:49 mlhuang Exp $
9 #
10
11 import sys
12 import traceback
13 import xmlrpclib
14 from mod_python import apache
15
16 from PLC.Debug import log
17
18 from PLC.API import PLCAPI
19 api = PLCAPI()
20
21 def handler(req):
22     try:
23         if req.method != "POST":
24             req.content_type = "text/html"
25             req.send_http_header()
26             req.write("""
27 <html><head>
28 <title>PLCAPI XML-RPC/SOAP Interface</title>
29 </head><body>
30 <h1>PLCAPI XML-RPC/SOAP Interface</h1>
31 <p>Please use XML-RPC or SOAP to access the PLCAPI.</p>
32 </body></html>
33 """)
34             return apache.OK
35
36         # Read request
37         request = req.read(int(req.headers_in['content-length']))
38
39         # mod_python < 3.2: The IP address portion of remote_addr is
40         # incorrect (always 0.0.0.0) when IPv6 is enabled.
41         # http://issues.apache.org/jira/browse/MODPYTHON-64?page=all
42         (remote_ip, remote_port) = req.connection.remote_addr
43         remote_addr = (req.connection.remote_ip, remote_port)
44
45         # Handle request
46         response = api.handle(remote_addr, request)
47
48         # Shut down database connection, otherwise up to MaxClients DB
49         # connections will remain open.
50         api.db.close()
51
52         # Write response
53         req.content_type = "text/xml; charset=" + api.encoding
54         req.send_http_header()
55         req.write(response)
56
57         return apache.OK
58
59     except Exception, err:
60         # Log error in /var/log/httpd/(ssl_)?error_log
61         print >> log, err, traceback.format_exc()
62         return apache.HTTP_INTERNAL_SERVER_ERROR