Skip to content

Instantly share code, notes, and snippets.

@ShailMurtaza
Last active October 25, 2025 01:46
Show Gist options
  • Save ShailMurtaza/ebf8eaaebc34802725562f57ad49d186 to your computer and use it in GitHub Desktop.
Save ShailMurtaza/ebf8eaaebc34802725562f57ad49d186 to your computer and use it in GitHub Desktop.
This Python Script will display host server latency and packet loss
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)
@ShailMurtaza
Copy link
Author

ShailMurtaza commented Oct 25, 2025

Replaced dictionary with list for jobs to print them in the same order as written in server.txt file in 3rd Revision

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment