3 # Simple standalone HTTP server for testing PLCAPI
5 # Mark Huang <mlhuang@cs.princeton.edu>
6 # Copyright (C) 2006 The Trustees of Princeton University
15 # Append PLC to the system path
16 sys.path.append(os.path.dirname(os.path.realpath(sys.argv[0])))
18 from PLC.API import PLCAPI
20 class PLCAPIRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
22 Simple standalone HTTP request handler for testing PLCAPI.
28 request = self.rfile.read(int(self.headers["Content-length"]))
31 response = self.server.api.handle(self.client_address, request)
34 self.send_response(200)
35 self.send_header("Content-type", "text/xml")
36 self.send_header("Content-length", str(len(response)))
38 self.wfile.write(response)
41 self.connection.shutdown(1)
45 sys.stderr.write(traceback.format_exc())
49 self.send_response(200)
50 self.send_header("Content-type", 'text/html')
54 <title>PLCAPI XML-RPC/SOAP Interface</title>
56 <h1>PLCAPI XML-RPC/SOAP Interface</h1>
57 <p>Please use XML-RPC or SOAP to access the PLCAPI.</p>
61 class PLCAPIServer(BaseHTTPServer.HTTPServer):
63 Simple standalone HTTP server for testing PLCAPI.
66 def __init__(self, addr, config):
67 self.api = PLCAPI(config)
68 self.allow_reuse_address = 1
69 BaseHTTPServer.HTTPServer.__init__(self, addr, PLCAPIRequestHandler)
74 config = "/etc/planetlab/plc_config"
77 print "Usage: %s [OPTION]..." % sys.argv[0]
79 print " -p PORT, --port=PORT TCP port number to listen on (default: %d)" % port
80 print " -f FILE, --config=FILE PLC configuration file (default: %s)" % config
81 print " -h, --help This message"
86 (opts, argv) = getopt.getopt(sys.argv[1:], "p:f:h", ["port=", "config=", "help"])
87 except getopt.GetoptError, err:
88 print "Error: " + err.msg
91 for (opt, optval) in opts:
92 if opt == "-p" or opt == "--port":
97 elif opt == "-f" or opt == "--config":
99 elif opt == "-h" or opt == "--help":
103 PLCAPIServer((addr, port), config).serve_forever()