import datetime as datetime from emailtool.emailer import Emailer from flask import Flask, render_template, request, redirect, url_for import configparser from gevent.pywsgi import WSGIServer import socket import logging config = configparser.ConfigParser() config.read('settings.ini') application_settings = config['application'] smtp_settings = config['smtp'] http_settings = config['http'] log = logging.getLogger('werkzeug') log.setLevel(logging.INFO) if application_settings['debug'].lower() == 'true': debug = True else: debug = False def send_email(to, subject, body): # Get settings from smtp_settings host = smtp_settings['host'] port = smtp_settings['port'] username = smtp_settings['username'] password = smtp_settings['password'] # Create an instance of the Emailer class emailer = Emailer(host, port, username, password) # Call the send_email method emailer.send_email(to, subject, body) app = Flask(__name__) # Create a route for the home page @app.route('/') def index(): # send_email('aday@twinfallspubliclibrary.org', 'TEST', 'This is a test email') return render_template('index.html') # on exit of the program make sure the http server is stopped #@app.teardown_appcontext def shutdown_session(exception=None): print('Stopping HTTP Service...') http_server.stop() # Get the systems hostname def get_hostname(): return socket.gethostname() # Get systems IP address def get_ip_address(): return socket.gethostbyname(socket.gethostname()) if __name__ == "__main__": print("------------------------- Start up -----------------------------") print("Starting HTTP Service on port %s..." % http_settings['port']) if debug is True: print("Debug mode is enabled.") http_server = WSGIServer(('0.0.0.0', int(http_settings['port'])), app) else: http_server = WSGIServer(('0.0.0.0', int(http_settings['port'])), app, log=log, error_log=log) print("HTTP Service Started.") print("--------------------- Application Details ---------------------") print("Application started at %s" % datetime.datetime.now()) print("System IP Address: %s" % get_ip_address()) print("System Hostname: %s" % get_hostname()) print("Access the Dashboard using a web browser using any of the following:") print("http://%s:%s or http://%s:%s" % (get_hostname(), http_settings['port'], get_ip_address(), http_settings['port'])) print("---------------------------------------------------------------") print("To stop the application close this window.") print("---------------------------------------------------------------") http_server.serve_forever()