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