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