Skip to content

Instantly share code, notes, and snippets.

@wamsachel
Last active November 21, 2016 19:15
Show Gist options
  • Select an option

  • Save wamsachel/e524614c7dc5c7f8afd099916db020b3 to your computer and use it in GitHub Desktop.

Select an option

Save wamsachel/e524614c7dc5c7f8afd099916db020b3 to your computer and use it in GitHub Desktop.
Simple web_app skeleton with arg/conf parsing
#!/usr/bin/env python3
"""
web_app.py
Author: wamsachel
Date: today
web_app is a simple bottle-drive web app which examines how to better handle internet vitriol,
rather than waiting for lawmakers to do so
Usage: $python3 web_app.py
"""
import argparse
from src.bottle import (abort, error, get, post, redirect, request, route, run, template, view, static_file)
import configparser
CONFIGURATION_FILE = './web_app.conf'
CONFIGURATION_SECTION = 'OPTIONS'
APP_DESCRIPTION = '''web_app is a simple bottle-drive web app which examines how to better handle internet vitriol,
rather than waiting for lawmakers to do so
'''
def start_bottle_server(**kwargs):
"""kicks off a bottle web server"""
if 'address' in kwargs:
srv_address = kwargs['address']
else:
raise Exception('[!] Missing argument: address')
if 'port' in kwargs:
srv_port = kwargs['port']
else:
raise Exception('[!] Missing argument: port')
if 'debug' in kwargs:
srv_debug = kwargs['debug']
else:
srv_debug = False
try:
run(host=srv_address, port=srv_port, debug=srv_debug, reloader=srv_debug)
except Exception as e:
raise e
def read_configuration_file(conf_file, conf_section):
""" handles configuration file reading, returns dict """
ret_conf = {}
config = configparser.ConfigParser()
try:
config.read(conf_file)
if conf_section not in config:
raise Exception('Invalid configuration file')
except Exception as e:
print (repr(e))
else:
ret_conf.update(config[conf_section])
return ret_conf
def read_args():
""" handles cli arg reading returns dict """
ret_args = {}
arg_parser = argparse.ArgumentParser(description=APP_DESCRIPTION)
arg_parser.add_argument('-a', '--address', required=False, help='Host name for server to run on (e.g 127.0.0.1 or 0.0.0.0)')
arg_parser.add_argument('-p', '--port', required=False, help='Port for web server to listen on')
arg_parser.add_argument('-d', '--debug', required=False, action='store_true', help='Run server with debugging options turned on')
try:
args = arg_parser.parse_args()
except Exception as e:
print(repr(e))
else:
if args.address is not None:
ret_args['address'] = args.address
if args.port is not None:
ret_args['port'] = args.port
if args.debug is not None:
ret_args['debug'] = args.debug
return ret_args
if __name__ == '__main__':
# Read in configuration settings, save into opts_dict
opts_dict = {}
conf_opts = read_configuration_file(CONFIGURATION_FILE, CONFIGURATION_SECTION)
opts_dict.update(conf_opts)
arg_opts = read_args()
opts_dict.update(arg_opts)
for opt in opts_dict:
print('{}:{}'.format(opt, opts_dict[opt]))
try:
start_bottle_server(**opts_dict)
except Exception as e:
print(repr(e))
print('[!] Dirty exit.')
else:
print('[*] Clean exit.')
finally:
print('[*] Finished...')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment