3 # Simple standalone HTTP server for testing PLCAPI
5 # Mark Huang <mlhuang@cs.princeton.edu>
6 # Copyright (C) 2006 The Trustees of Princeton University
18 # Append PLC to the system path
19 sys.path.append(os.path.dirname(os.path.realpath(sys.argv[0])))
21 from PLC.API import PLCAPI
23 class PLCAPIRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
25 Simple standalone HTTP request handler for testing PLCAPI.
31 request = self.rfile.read(int(self.headers["Content-length"]))
34 response = self.server.api.handle(self.client_address, request)
37 self.send_response(200)
38 self.send_header("Content-type", "text/xml")
39 self.send_header("Content-length", str(len(response)))
41 self.wfile.write(response)
44 self.connection.shutdown(1)
48 sys.stderr.write(traceback.format_exc())
52 self.send_response(200)
53 self.send_header("Content-type", 'text/html')
57 <title>PLCAPI XML-RPC/SOAP Interface</title>
59 <h1>PLCAPI XML-RPC/SOAP Interface</h1>
60 <p>Please use XML-RPC or SOAP to access the PLCAPI.</p>
64 class PLCAPIServer(BaseHTTPServer.HTTPServer):
66 Simple standalone HTTP server for testing PLCAPI.
69 def __init__(self, addr, config):
70 self.api = PLCAPI(config)
71 self.allow_reuse_address = 1
72 BaseHTTPServer.HTTPServer.__init__(self, addr, PLCAPIRequestHandler)
77 config = "/etc/planetlab/plc_config"
80 print "Usage: %s [OPTION]..." % sys.argv[0]
82 print " -p PORT, --port=PORT TCP port number to listen on (default: %d)" % port
83 print " -f FILE, --config=FILE PLC configuration file (default: %s)" % config
84 print " -h, --help This message"
89 (opts, argv) = getopt.getopt(sys.argv[1:], "p:f:h", ["port=", "config=", "help"])
90 except getopt.GetoptError, err:
91 print "Error: " + err.msg
94 for (opt, optval) in opts:
95 if opt == "-p" or opt == "--port":
100 elif opt == "-f" or opt == "--config":
102 elif opt == "-h" or opt == "--help":
106 PLCAPIServer((addr, port), config).serve_forever()