3 # Standalone WSGI PLCAPI Server
5 # Tony Mack <tmack@cs.princeton.edu>
6 # Copyright (C) 2006 The Trustees of Princeton University
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
23 def __init__(self, api=None):
28 def __call__(self, *args, **kwds):
29 return self.handle(*args, **kwds)
31 def handle(self, env, start_response):
32 if env['REQUEST_METHOD'] == 'POST':
33 return self.do_post(env, start_response)
35 return self.do_get(env, start_response)
37 def do_get(self, env, start_response):
40 <title>PLCAPI Nova XML-RPC/SOAP Interface</title>
42 <h1>PLCAPI XML-RPC/SOAP Interface</h1>
43 <p>Please use XML-RPC or SOAP to access the PLCAPI.</p>
47 headers = [('Content-Type', 'text/html')]
48 start_response(status, headers)
51 def do_post(self, env, start_response):
53 request_size = int(env.get('CONTENT_LENGTH', 0))
56 request = env['wsgi.input'].read(request_size)
57 client_address = env['REMOTE_ADDR']
60 response = self.api.handle(client_address, request)
62 headers = [('Content-Type', 'text/html')]
64 response = "'<h1>Internal Server Error</h1>"
65 status = '500 Internal Server Error'
66 headers = [('Content-Type', 'text/html')]
67 logger.log_exc(status)
69 start_response(status, headers)
76 config_file = '/etc/planetlab/plcapi_config'
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')
88 (options, args) = parser.parse_args()
90 print "Usage: %s [OPTION]..." % sys.argv[0]
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"
98 config = Config(options.config)
99 addr = config.api_host
100 keyfile = config.api_ssl_key
101 certfile = config.api_ssl_cert
104 port = int(options.port)
106 if keyfile and certfile:
107 server = pywsgi.WSGIServer((addr, port), App(), keyfile=keyfile, certfile=certfile)
109 server = pywsgi.WSGIServer((addr, port), App())
111 server.serve_forever()