app.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import datetime as datetime
  2. from emailtool.emailer import Emailer
  3. from flask import Flask, render_template, request, redirect, url_for
  4. import configparser
  5. from gevent.pywsgi import WSGIServer
  6. import socket
  7. config = configparser.ConfigParser()
  8. config.read('settings.ini')
  9. smtp_settings = config['smtp']
  10. http_settings = config['http']
  11. def send_email(to, subject, body):
  12. # Get settings from smtp_settings
  13. host = smtp_settings['host']
  14. port = smtp_settings['port']
  15. username = smtp_settings['username']
  16. password = smtp_settings['password']
  17. # Create an instance of the Emailer class
  18. emailer = Emailer(host, port, username, password)
  19. # Call the send_email method
  20. emailer.send_email(to, subject, body)
  21. app = Flask(__name__)
  22. # Create a route for the home page
  23. @app.route('/')
  24. def index():
  25. # send_email('aday@twinfallspubliclibrary.org', 'TEST', 'This is a test email')
  26. return render_template('index.html')
  27. # on exit of the program make sure the http server is stopped
  28. #@app.teardown_appcontext
  29. def shutdown_session(exception=None):
  30. print('Stopping HTTP Service...')
  31. http_server.stop()
  32. # Get the systems hostname
  33. def get_hostname():
  34. return socket.gethostname()
  35. # Get systems IP address
  36. def get_ip_address():
  37. return socket.gethostbyname(socket.gethostname())
  38. if __name__ == "__main__":
  39. print("Starting HTTP Service on port %s..." % http_settings['port'])
  40. http_server = WSGIServer(('0.0.0.0', int(http_settings['port'])), app)
  41. print("HTTP Service Started!")
  42. print("Access the Dashboard on http://%s:%s" % (get_hostname(), http_settings['port']))
  43. http_server.serve_forever()