aboutsummaryrefslogtreecommitdiffstats
path: root/bin
diff options
context:
space:
mode:
authorAaron LI <aaronly.me@outlook.com>2016-11-08 19:08:20 +0800
committerAaron LI <aaronly.me@outlook.com>2016-11-08 19:11:42 +0800
commitfca4b4700a12b26c623f0dfd5f83e7342567bb10 (patch)
tree595f00910c22cc1a2ebf10286ff8118e9ff34fef /bin
parenta515befec473b578d4f4f2de4e2a7d4bc8202e83 (diff)
downloadfg21sim-fca4b4700a12b26c623f0dfd5f83e7342567bb10.tar.bz2
bin/fg21sim-webui: Use "tornado.options" instead of "argparse"
* The "tornado.options" can be used in the *global scope*, therefore, the command line arguments can be stored in the options and then import the options in other modules if needed. * Add argument "--hosts-allowed", which specifies the hosts/network can access the Web UI (i.e., WebSocket) * Add argument "--no-browser", which controls whether to open the Web UI in a browser after startup. (TODO)
Diffstat (limited to 'bin')
-rwxr-xr-xbin/fg21sim-webui46
1 files changed, 34 insertions, 12 deletions
diff --git a/bin/fg21sim-webui b/bin/fg21sim-webui
index 8fc7673..b977bc6 100755
--- a/bin/fg21sim-webui
+++ b/bin/fg21sim-webui
@@ -17,35 +17,57 @@ which is built using the Tornado_ web server and WebSocket_ protocol.
import os
import sys
-import argparse
import logging
+import ipaddress
import tornado.ioloop
+from tornado.options import define, options, parse_command_line
from fg21sim.configs import configs
from fg21sim.utils import setup_logging
from fg21sim.webui import make_application
+# Define options in the global namespace
+# These options can also be used in other modules
+define("port", default=21127, type=int, help="Server listen port")
+define("debug", default=False, help="Enable the debug mode")
+define("no_browser", default=False,
+ help="Do not open the Web UI in a browser after startup")
+define("hosts_allowed", default="any", type=str,
+ help=("Hosts allowed to access the Web UI. "
+ "The network addresses should be given in CIDR format, e.g., "
+ "'192.168.0.0/24'. "
+ "Specify 'any' to allow any hosts. "
+ "Note that the localhost/127.0.0.1 is always allowed."))
+
+
def main():
- parser = argparse.ArgumentParser(description="Start the fg21sim Web UI")
- parser.add_argument("-p", "--port", type=int, default=21127,
- help="Tornado server listen port (default: 21127)")
- parser.add_argument("-d", "--debug", action="store_true",
- help="enable Tornado debug mode")
- args = parser.parse_args()
-
- loglevel = "DEBUG" if args.debug else None
+ options.logging = None
+ parse_command_line()
+
+ # Validate the value of ``options.hosts_allowed``
+ if options.hosts_allowed.upper() != "ANY":
+ try:
+ ipaddress.ip_network(options.hosts_allowed)
+ except ValueError as e:
+ raise ValueError("Option 'hosts_allowed' invalid: " + str(e))
+
+ loglevel = "DEBUG" if options.debug else None
setup_logging(dict_config=configs.logging, level=loglevel)
tool = os.path.basename(sys.argv[0])
logger = logging.getLogger(tool)
logger.info("COMMAND: {0}".format(" ".join(sys.argv)))
- application = make_application(debug=args.debug)
- application.listen(args.port)
+ application = make_application(debug=options.debug)
+ application.listen(options.port)
logger.info("Tornado started on: {protocol}://{host}:{port}".format(
- protocol="http", host="localhost", port=args.port))
+ protocol="http", host="localhost", port=options.port))
+ logger.info("Hosts allowed to access the Web UI: {0}".format(
+ options.hosts_allowed))
+ print("DEBUG: before IOLoop")
tornado.ioloop.IOLoop.current().start()
+ print("DEBUG: after IOLoop")
if __name__ == "__main__":