Last active
October 25, 2025 01:46
-
-
Save ShailMurtaza/ebf8eaaebc34802725562f57ad49d186 to your computer and use it in GitHub Desktop.
This Python Script will display host server latency and packet loss
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from ping3 import ping | |
| from concurrent.futures import ThreadPoolExecutor, as_completed | |
| import tabulate | |
| from time import sleep, time | |
| from os import system | |
| tabulate.PRESERVE_WHITESPACE = True | |
| def get_server(): | |
| with open("servers.txt", "r") as r: | |
| data = r.read() | |
| data = data.strip().split("\n") | |
| data = [x.split(" ") for x in data] | |
| return data | |
| def ping_host(host): | |
| attempts = 3 | |
| success = 0 | |
| for i in range(attempts): | |
| res = ping(host, unit='ms') | |
| if res: | |
| success += 1 | |
| packet_loss = (attempts - success) / attempts * 100 | |
| return (res, packet_loss) | |
| def ping_list(servers): | |
| results = [] | |
| with ThreadPoolExecutor(max_workers=10) as executor: | |
| futures = [executor.submit(ping_host, host) for _, host in servers] | |
| for i, future in enumerate(as_completed(futures)): | |
| name = servers[i][0] | |
| res, packet_loss = future.result() | |
| if res: | |
| results.append((name, f"{res:.2f} ms", f"{packet_loss:.2f}%")) | |
| else: | |
| results.append((name, "!!!!", f"{packet_loss:.2f}%")) | |
| return tabulate.tabulate(results, headers=("Host", "Ping", "Packet Loss"), tablefmt="rounded_outline") | |
| iterations = 0 | |
| servers = get_server() | |
| while True: | |
| prev_time = time() | |
| table = ping_list(servers) | |
| new_time = time() | |
| iterations += 1 | |
| system("cls") | |
| print(f"{iterations} | {new_time-prev_time:.2f}s") | |
| print(table) | |
| sleep(0.5) |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Replaced dictionary with list for jobs to print them in the same order as written in
server.txtfile in 3rd Revision