diff --git a/ping_engine.py b/ping_engine.py index be5738d..9ec0b72 100644 --- a/ping_engine.py +++ b/ping_engine.py @@ -4,6 +4,7 @@ from markupsafe import escape import os import requests +import datetime app = Flask(__name__) @@ -18,18 +19,44 @@ def hello(): @app.route('/ping/') def ping(name): - personal = f'pinging, {name}' - response = os.system("ping -c 1 " + name) + now = str(datetime.datetime.now()) if response == 0: - instruc = ' is up!' - address = 'http://%s' %name + instruc = ' up' + else: + instruc = ' down' + return now + instruc + +@app.route('/http/') +def http(name): + address = 'http://%s' %name + now = str(datetime.datetime.now()) + try: r = requests.get(address) status = r.status_code - status_string = str(status) - else: - instruc = ' is down!' - return personal + instruc + status_string + status_string = ' %s' %str(status) + except requests.exceptions.Timeout: + status_string = " Timeout" + except requests.exceptions.TooManyRedirects: + status_string = " TooManyRedirects" + except requests.exceptions.RequestException as e: + status_string = ' %s' %str(e) + return now + status_string + +@app.route('/https/') +def https(name): + address = 'https://%s' %name + try: + r = requests.get(address) + status = r.status_code + status_string = ' %s' %str(status) + except requests.exceptions.Timeout: + status_string = " Timeout" + except requests.exceptions.TooManyRedirects: + status_string = " TooManyRedirects" + except requests.exceptions.RequestException as e: + status_string = ' %s' %str(e) + return now + status_string if __name__ == '__main__': app.run(debug=True) \ No newline at end of file