3 # Simple standalone HTTP server for testing PLCAPI
5 # Mark Huang <mlhuang@cs.princeton.edu>
6 # Copyright (C) 2006 The Trustees of Princeton University
8 # $Id: Server.py,v 1.3 2006/10/25 20:33:07 mlhuang Exp $
17 # Append PLC to the system path
18 sys.path.append(os.path.dirname(os.path.realpath(sys.argv[0])))
20 from PLC.API import PLCAPI
22 class PLCAPIRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
24 Simple standalone HTTP request handler for testing PLCAPI.
30 request = self.rfile.read(int(self.headers["Content-length"]))
33 response = self.server.api.handle(self.client_address, request)
36 self.send_response(200)
37 self.send_header("Content-type", "text/xml")
38 self.send_header("Content-length", str(len(response)))
40 self.wfile.write(response)
43 self.connection.shutdown(1)
47 sys.stderr.write(traceback.format_exc())
51 self.send_response(200)
52 self.send_header("Content-type", 'text/html')
56 <title>PLCAPI XML-RPC/SOAP Interface</title>
58 <h1>PLCAPI XML-RPC/SOAP Interface</h1>
59 <p>Please use XML-RPC or SOAP to access the PLCAPI.</p>
63 class PLCAPIServer(BaseHTTPServer.HTTPServer):
65 Simple standalone HTTP server for testing PLCAPI.
68 def __init__(self, addr, config):
69 self.api = PLCAPI(config)
70 self.allow_reuse_address = 1
71 BaseHTTPServer.HTTPServer.__init__(self, addr, PLCAPIRequestHandler)
76 config = "/etc/planetlab/plc_config"
79 print "Usage: %s [OPTION]..." % sys.argv[0]
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"
88 (opts, argv) = getopt.getopt(sys.argv[1:], "p:f:h", ["port=", "config=", "help"])
89 except getopt.GetoptError, err:
90 print "Error: " + err.msg
93 for (opt, optval) in opts:
94 if opt == "-p" or opt == "--port":
99 elif opt == "-f" or opt == "--config":
101 elif opt == "-h" or opt == "--help":
105 PLCAPIServer((addr, port), config).serve_forever()