Merge remote-tracking branch 'origin/pycurl' into planetlab-4_0-branch
[plcapi.git] / Server.py
1 #!/usr/bin/python
2 #
3 # Simple standalone HTTP server for testing PLCAPI
4 #
5 # Mark Huang <mlhuang@cs.princeton.edu>
6 # Copyright (C) 2006 The Trustees of Princeton University
7 #
8 # $Id: Server.py,v 1.3 2006/10/25 20:33:07 mlhuang Exp $
9 #
10
11 import os
12 import sys
13 import getopt
14 import traceback
15 import BaseHTTPServer
16
17 # Append PLC to the system path
18 sys.path.append(os.path.dirname(os.path.realpath(sys.argv[0])))
19
20 from PLC.API import PLCAPI
21
22 class PLCAPIRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
23     """
24     Simple standalone HTTP request handler for testing PLCAPI.
25     """
26
27     def do_POST(self):
28         try:
29             # Read request
30             request = self.rfile.read(int(self.headers["Content-length"]))
31
32             # Handle request
33             response = self.server.api.handle(self.client_address, request)
34
35             # Write response
36             self.send_response(200)
37             self.send_header("Content-type", "text/xml")
38             self.send_header("Content-length", str(len(response)))
39             self.end_headers()
40             self.wfile.write(response)
41
42             self.wfile.flush()
43             self.connection.shutdown(1)
44
45         except Exception, e:
46             # Log error
47             sys.stderr.write(traceback.format_exc())
48             sys.stderr.flush()
49
50     def do_GET(self):
51         self.send_response(200)
52         self.send_header("Content-type", 'text/html')
53         self.end_headers()
54         self.wfile.write("""
55 <html><head>
56 <title>PLCAPI XML-RPC/SOAP Interface</title>
57 </head><body>
58 <h1>PLCAPI XML-RPC/SOAP Interface</h1>
59 <p>Please use XML-RPC or SOAP to access the PLCAPI.</p>
60 </body></html>
61 """)        
62         
63 class PLCAPIServer(BaseHTTPServer.HTTPServer):
64     """
65     Simple standalone HTTP server for testing PLCAPI.
66     """
67
68     def __init__(self, addr, config):
69         self.api = PLCAPI(config)
70         self.allow_reuse_address = 1
71         BaseHTTPServer.HTTPServer.__init__(self, addr, PLCAPIRequestHandler)
72
73 # Defaults
74 addr = "0.0.0.0"
75 port = 8000
76 config = "/etc/planetlab/plc_config"
77
78 def usage():
79     print "Usage: %s [OPTION]..." % sys.argv[0]
80     print "Options:"
81     print "     -p PORT, --port=PORT    TCP port number to listen on (default: %d)" % port
82     print "     -f FILE, --config=FILE  PLC configuration file (default: %s)" % config
83     print "     -h, --help              This message"
84     sys.exit(1)
85
86 # Get options
87 try:
88     (opts, argv) = getopt.getopt(sys.argv[1:], "p:f:h", ["port=", "config=", "help"])
89 except getopt.GetoptError, err:
90     print "Error: " + err.msg
91     usage()
92
93 for (opt, optval) in opts:
94     if opt == "-p" or opt == "--port":
95         try:
96             port = int(optval)
97         except ValueError:
98             usage()
99     elif opt == "-f" or opt == "--config":
100         config = optval
101     elif opt == "-h" or opt == "--help":
102         usage()
103
104 # Start server
105 PLCAPIServer((addr, port), config).serve_forever()