From 63344a39f61e68ba550a4ca9deddca4ae554b645 Mon Sep 17 00:00:00 2001 From: Domagoj Zecevic Date: Thu, 10 Nov 2022 21:12:08 +0100 Subject: [PATCH] added json output --- ping_engine.py | 37 ++++++++++++++++++++++--------------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/ping_engine.py b/ping_engine.py index 19d728f..0604d69 100644 --- a/ping_engine.py +++ b/ping_engine.py @@ -1,5 +1,5 @@ from glob import escape -from flask import Flask,request,json +from flask import Flask,request,jsonify from markupsafe import escape import os @@ -26,15 +26,17 @@ def ping(name): if apiToken == token: response = os.system("ping -c 1 " + name) if response == 0: - instruc = ' up' + instruc = 'up' else: - instruc = ' down' + instruc = 'down' else: instruc = ' wrong token' ip_addr = request.remote_addr out = '%s - Unauthorized from IP: %s' %(now, ip_addr) print (out) - return now + instruc + return jsonify( + time = now, + status = instruc) @app.route('/http//') def http(name): @@ -45,19 +47,21 @@ def http(name): try: r = requests.get(address) status = r.status_code - status_string = ' %s' %str(status) + status_string = '%s' %str(status) except requests.exceptions.Timeout: - status_string = " Timeout" + status_string = "Timeout" except requests.exceptions.TooManyRedirects: - status_string = " TooManyRedirects" + status_string = "TooManyRedirects" except requests.exceptions.RequestException as e: - status_string = ' %s' %str(e) + status_string = '%s' %str(e) else: - status_string = ' wrong token' + status_string = 'wrong token' ip_addr = request.remote_addr out = '%s - Unauthorized from IP: %s' %(now, ip_addr) print (out) - return now + status_string + return jsonify ( + time = now, + status = status_string) @app.route('/https//') def https(name): @@ -68,19 +72,22 @@ def https(name): try: r = requests.get(address) status = r.status_code - status_string = ' %s' %str(status) + status_string = '%s' %str(status) except requests.exceptions.Timeout: - status_string = " Timeout" + status_string = "Timeout" except requests.exceptions.TooManyRedirects: - status_string = " TooManyRedirects" + status_string = "TooManyRedirects" except requests.exceptions.RequestException as e: - status_string = ' %s' %str(e) + status_string = '%s' %str(e) else: status_string = ' wrong token' ip_addr = request.remote_addr out = '%s - Unauthorized from IP: %s' %(now, ip_addr) print (out) - return now + status_string + return jsonify ( + time = now, + status = status_string) + if __name__ == '__main__': app.run(debug=True) \ No newline at end of file