import repository from arizona
[raven.git] / lib / ravenlib / modpythonapi / server.py
1 #!/usr/bin/python
2 #
3 # Adapted from PLCAPI/Server.py
4 #
5
6 import os
7 import sys
8 import getopt
9 import traceback
10 import BaseHTTPServer
11
12 # Defaults
13 glo_addr = "0.0.0.0"
14 glo_port = 8000
15
16 class StandaloneRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
17     """
18     Simple standalone HTTP request handler for testing API.
19     """
20
21     def do_POST(self):
22         try:
23             # Read request
24             request = self.rfile.read(int(self.headers["Content-length"]))
25
26             # Handle request
27             response = self.server.api.handle(self.client_address, request)
28
29             # Write response
30             self.send_response(200)
31             self.send_header("Content-type", "text/xml")
32             self.send_header("Content-length", str(len(response)))
33             self.end_headers()
34             self.wfile.write(response)
35
36             self.wfile.flush()
37             self.connection.shutdown(1)
38
39         except Exception, e:
40             # Log error
41             sys.stderr.write(traceback.format_exc())
42             sys.stderr.flush()
43
44     def do_GET(self):
45         self.send_response(200)
46         self.send_header("Content-type", 'text/html')
47         self.end_headers()
48         self.wfile.write("""
49 <html><head>
50 <title>PLCAPI XML-RPC/SOAP Interface</title>
51 </head><body>
52 <h1>PLCAPI XML-RPC/SOAP Interface</h1>
53 <p>Please use XML-RPC or SOAP to access the PLCAPI.</p>
54 </body></html>
55 """)
56
57 class StandaloneServer(BaseHTTPServer.HTTPServer):
58     """
59     Simple standalone HTTP server for testing PLCAPI.
60     """
61
62     def __init__(self, addr, api):
63         self.api = api
64         self.allow_reuse_address = 1
65         BaseHTTPServer.HTTPServer.__init__(self, addr, StandaloneRequestHandler)
66
67 def usage():
68     print "Usage: %s [OPTION]..." % sys.argv[0]
69     print "Options:"
70     print "     -p PORT, --port=PORT    TCP port number to listen on (default: %d)" % port
71     print "     -h, --help              This message"
72     sys.exit(1)
73
74 def get_options():
75     global glo_addr, glo_port
76
77     # Get options
78     try:
79         (opts, argv) = getopt.getopt(sys.argv[1:], "p:f:h", ["port=", "help"])
80     except getopt.GetoptError, err:
81         print "Error: " + err.msg
82         usage()
83
84     for (opt, optval) in opts:
85         if opt == "-p" or opt == "--port":
86             try:
87                 glo_port = int(optval)
88             except ValueError:
89                 usage()
90         elif opt == "-h" or opt == "--help":
91             usage()
92
93 def main():
94     global glo_addr, glo_port
95
96     from API import RemoteApi()
97
98     get_options()
99
100     api = RemoteApi()
101
102     StandaloneServer((glo_addr, glo_port), api).serve_forever()
103
104 if __name__=="__main__":
105    main()