1
0

app.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. import logging
  8. config = configparser.ConfigParser()
  9. config.read('settings.ini')
  10. application_settings = config['application']
  11. smtp_settings = config['smtp']
  12. http_settings = config['http']
  13. log = logging.getLogger('werkzeug')
  14. log.setLevel(logging.INFO)
  15. if application_settings['debug'].lower() == 'true':
  16. debug = True
  17. else:
  18. debug = False
  19. def send_email(to, subject, body):
  20. # Get settings from smtp_settings
  21. host = smtp_settings['host']
  22. port = smtp_settings['port']
  23. username = smtp_settings['username']
  24. password = smtp_settings['password']
  25. # Create an instance of the Emailer class
  26. emailer = Emailer(host, port, username, password)
  27. # Call the send_email method
  28. emailer.send_email(to, subject, body)
  29. app = Flask(__name__)
  30. # Create a route for the home page
  31. @app.route('/')
  32. def index():
  33. # send_email('aday@twinfallspubliclibrary.org', 'TEST', 'This is a test email')
  34. return render_template('index.html')
  35. # on exit of the program make sure the http server is stopped
  36. #@app.teardown_appcontext
  37. def shutdown_session(exception=None):
  38. print('Stopping HTTP Service...')
  39. http_server.stop()
  40. # Get the systems hostname
  41. def get_hostname():
  42. return socket.gethostname()
  43. # Get systems IP address
  44. def get_ip_address():
  45. return socket.gethostbyname(socket.gethostname())
  46. if __name__ == "__main__":
  47. print("------------------------- Start up -----------------------------")
  48. print("Starting HTTP Service on port %s..." % http_settings['port'])
  49. if debug is True:
  50. print("Debug mode is enabled.")
  51. http_server = WSGIServer(('0.0.0.0', int(http_settings['port'])), app)
  52. else:
  53. http_server = WSGIServer(('0.0.0.0', int(http_settings['port'])), app, log=log, error_log=log)
  54. print("HTTP Service Started.")
  55. print("--------------------- Application Details ---------------------")
  56. print("Application started at %s" % datetime.datetime.now())
  57. print("System IP Address: %s" % get_ip_address())
  58. print("System Hostname: %s" % get_hostname())
  59. print("Access the Dashboard using a web browser using any of the following:")
  60. print("http://%s:%s or http://%s:%s" % (get_hostname(), http_settings['port'], get_ip_address(), http_settings['port']))
  61. print("---------------------------------------------------------------")
  62. print("To stop the application close this window.")
  63. print("---------------------------------------------------------------")
  64. http_server.serve_forever()