checking in
[plcapi.git] / Server.py
1 #!/usr/bin/python
2 #
3 # Standalone WSGI PLCAPI Server
4 #
5 # Tony Mack <tmack@cs.princeton.edu>
6 # Copyright (C) 2006 The Trustees of Princeton University
7 #
8
9 import os
10 import sys
11 import traceback
12 from optparse import OptionParser
13 from gevent import pywsgi
14 # Append PLC to the system path
15 sys.path.append(os.path.dirname(os.path.realpath(sys.argv[0])))
16 #from PLC.API import PLCAPI
17
18
19 class App:
20
21     def __init__(self, api=None):
22         self.api = api
23
24     def __call__(self, *args, **kwds):
25         return self.handle(*args, **kwds)        
26
27     def handle(self, env, start_response):
28         if env['REQUEST_METHOD'] == 'POST':
29             return self.do_post(env, start_response)
30         else:
31             return self.do_get(env, start_response)
32            
33     def do_get(self, env, start_response):
34         response = """
35 <html><head>
36 <title>PLCAPI Nova XML-RPC/SOAP Interface</title>
37 </head><body>
38 <h1>PLCAPI XML-RPC/SOAP Interface</h1>
39 <p>Please use XML-RPC or SOAP to access the PLCAPI.</p>
40 </body></html>
41 """
42         status = '200 OK'
43         headers = [('Content-Type', 'text/html')]
44         start_response(status, headers)
45         return [response]
46
47     def do_post(self, env, start_response):      
48         try:
49             request_size = int(environ.get('CONTENT_LENGTH', 0))
50         except (ValueError):
51             request_size = 0
52         request = environ['wsgi.input'].read(request_size)
53         client_address = environ['REMOTE_ADDR'] 
54  
55         try: 
56             response = self.api.handle(client_address, request) 
57             status = '200 OK'
58             headers = [('Content-Type', 'text/html')]
59         except:
60             response = "'<h1>Internal Server Error</h1>"
61             status = '500 Internal Server Error'
62             headers = [('Content-Type', 'text/html')]
63             
64         start_response(status, headers)
65         return [response] 
66
67
68 # Defaults
69 addr = "0.0.0.0"
70 port = 8000
71 config = '/etc/planetlab/plc_config'
72 keyfile=None
73 certfile=None
74
75
76 # Get options
77 parser = OptionParser()
78 parser.add_option('-p', '--port', dest='port', metavar='<port>', help='TCP port number to listen on')
79 parser.add_option('-f', '--config', dest='config', metavar='<config>', help='PLCAPI configuration file')
80 options = None
81 args = None 
82 try:
83     (options, args) = parser.parse_args()
84 except:
85     print "Usage: %s [OPTION]..." % sys.argv[0]
86     print "Options:"
87     print "     -p PORT, --port=PORT    TCP port number to listen on (default: %d)" % port
88     print "     -f FILE, --config=FILE  PLC configuration file (default: %s)" % config
89     print "     -h, --help              This message"
90     sys.exit(1) 
91
92 if options.config:
93     config = Config(options.config)
94     addr = config
95     keyfile = config.api_ssl_key_file
96     certfile = config.api_ssl_cert_file 
97     
98
99 if options.port:
100     port = int(options.port)
101
102 # Start server
103 if keyfile and certfile:
104     server = pywsgi.WSGIServer((addr, port), App(), keyfile=keyfile, certfile=certfile)
105 else:
106     server = pywsgi.WSGIServer((addr, port), App())
107     
108 server.serve_forever()