added create_network(), delete_network(), create_subnet(), delete_subnet(), process_t...
[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 from PLC.Config import Config
18 from PLC.Logger import logger
19
20
21 class App:
22
23     def __init__(self, api=None):
24         if not api:
25             api = PLCAPI()  
26         self.api = api
27
28     def __call__(self, *args, **kwds):
29         return self.handle(*args, **kwds)        
30
31     def handle(self, env, start_response):
32         if env['REQUEST_METHOD'] == 'POST':
33             return self.do_post(env, start_response)
34         else:
35             return self.do_get(env, start_response)
36            
37     def do_get(self, env, start_response):
38         response = """
39 <html><head>
40 <title>PLCAPI Nova XML-RPC/SOAP Interface</title>
41 </head><body>
42 <h1>PLCAPI XML-RPC/SOAP Interface</h1>
43 <p>Please use XML-RPC or SOAP to access the PLCAPI.</p>
44 </body></html>
45 """
46         status = '200 OK'
47         headers = [('Content-Type', 'text/html')]
48         start_response(status, headers)
49         return [response]
50
51     def do_post(self, env, start_response):      
52         try:
53             request_size = int(env.get('CONTENT_LENGTH', 0))
54         except (ValueError):
55             request_size = 0
56         request = env['wsgi.input'].read(request_size)
57         client_address = env['REMOTE_ADDR'] 
58  
59         try: 
60             response = self.api.handle(client_address, request) 
61             status = '200 OK'
62             headers = [('Content-Type', 'text/html')]
63         except:
64             response = "'<h1>Internal Server Error</h1>"
65             status = '500 Internal Server Error'
66             headers = [('Content-Type', 'text/html')]
67             logger.log_exc(status)
68             
69         start_response(status, headers)
70         return [response] 
71
72
73 # Defaults
74 addr = "0.0.0.0"
75 port = 8000
76 config_file = '/etc/planetlab/plcapi_config'
77 keyfile=None
78 certfile=None
79
80
81 # Get options
82 parser = OptionParser()
83 parser.add_option('-p', '--port', dest='port', metavar='<port>', help='TCP port number to listen on')
84 parser.add_option('-f', '--config', dest='config', metavar='<config>', help='PLCAPI configuration file')
85 options = None
86 args = None 
87 try:
88     (options, args) = parser.parse_args()
89 except:
90     print "Usage: %s [OPTION]..." % sys.argv[0]
91     print "Options:"
92     print "     -p PORT, --port=PORT    TCP port number to listen on (default: %d)" % port
93     print "     -f FILE, --config=FILE  PLC configuration file (default: %s)" % config
94     print "     -h, --help              This message"
95     sys.exit(1) 
96
97 if options.config:
98     config = Config(options.config)
99     addr = config.api_host
100     keyfile = config.api_ssl_key
101     certfile = config.api_ssl_cert
102  
103 if options.port:
104     port = int(options.port)
105
106 if keyfile and certfile:
107     server = pywsgi.WSGIServer((addr, port), App(), keyfile=keyfile, certfile=certfile)
108 else:
109     server = pywsgi.WSGIServer((addr, port), App())
110     
111 server.serve_forever()