Example #1
0
def main(argv):
    """Main entrypoint."""
 
    try:
 
        # Parse command-line options
        parser = OptionParser()
        parser.add_option("-p", "--port", dest="port", metavar="PORT",
                          action="store", type="int", default=9959,
                          help="port number on which to listen")
        parser.add_option("-t", "--terminate-after", dest="termafter",
                          metavar="NUM", action="store", type="int", default=0,
                          help="number of requests after which to terminate")
        options, args = parser.parse_args(argv[1:])
 
        # Create server instance
        httpd = HTTPServer(("localhost", options.port), StatusManagerServer)
        if hasattr(httpd, "active_list"):
            raise Exception("HTTP server instance already has 'active_list' attribute")
        httpd.active_list = {}
 
        # Serve requests
        if options.termafter == 0:
            httpd.serve_forever()
        else:
            for i in xrange(options.termafter):
                httpd.handle_request()
 
    except Exception:
        return 1
 
    return 0