Added ping.1m+.py

This commit is contained in:
Tobias Schmidl 2020-09-21 08:10:03 +02:00
parent 538b7b6efb
commit 3a8c260b8c

74
ping.1m+.py Executable file
View file

@ -0,0 +1,74 @@
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
""" Simulates httping for `argos <https://github.com/p-e-w/argos>`"""
import sys #for redirecting print to stderr
import datetime #for the whole ms → s and vice versa stuff
import requests #for the central HTTP HEAD request
import statistics #for median and stdev
__author__ = "Tobias Schmidl"
__copyright__ = "Copyright 2018, Tobias Schmidl"
__license__ = "MIT"
__version__ = "0.0.1"
__maintainer__ = "Tobias Schmidl"
# in seconds
max_ping = datetime.timedelta(milliseconds = 1000)
targets = ["https://www.google.de", "https://www.bing.com", "https://www.wikipedia.org" ]
max_ping_in_ms = max_ping.total_seconds() * 1000
class Themes:
purple_green = ["#762a83", "#9970ab","#c2a5cf","#a6dba0","#5aae61","#1b7837"]
red_green = ["#d73027", "#fc8d59","#fee08b","#d9ef8b","#91cf60","#1a9850"]
# shellcheck disable=SC2034
original = ["#acacac","#ff0101","#cc673b","#ce8458","#6bbb15","#0ed812"]
# Configuration
selected_colors = red_green
def colorize(response_time):
# panda's "cut" functon would be an alternative here, but this would be overkill, to include numpy just for this
if response_time < max_ping_in_ms / 5:
return Themes.selected_colors[5]
elif response_time >= max_ping_in_ms / 5 and response_time < 2 * max_ping_in_ms / 5:
return Themes.selected_colors[4]
elif response_time >= 2 * max_ping_in_ms / 5 and response_time < 3 * max_ping_in_ms / 5:
return Themes.selected_colors[3]
elif response_time >= 3 * max_ping_in_ms / 5 and response_time < 4 * max_ping_in_ms / 5:
return Themes.selected_colors[2]
elif response_time >= 4 * max_ping_in_ms / 5 and response_time < max_ping_in_ms:
return Themes.selected_colors[1]
else:
return Themes.selected_colors[0]
def httping(targets):
responses = {}
for target in targets:
try:
responses[target] = requests.head(target, timeout = max_ping.total_seconds()).elapsed.total_seconds() * 1000
except requests.exceptions.RequestException as ex:
print(str(ex), file=sys.stderr)
responses[target] = None
response_times = [value for key, value in responses.items() if isinstance(value, float)]
if len(response_times) > 1:
return responses, statistics.median(response_times), statistics.stdev(response_times)
elif len(response_times) > 0:
return responses, response_times[0], 0
else:
return responses, None, None
if __name__ == "__main__":
response_times, median, stddev = httping(targets)
if isinstance(median, float) and median != max_ping_in_ms:
print("⦿ " + str(round(median,2)) + "±" + str(round(stddev, 2)) + " | color=" + Themes.colorize(median))
else:
print("☠ | color=" + Themes.selected_colors[0]);
print("---")
for target, response_time in response_times.items():
if isinstance(response_time, float):
print(target + ": " + str(round(response_time, 2)) + " | color=" + Themes.colorize(response_time))
else:
print(target + ": ☠ | color=" + Themes.selected_colors[0])