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