-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspeedtest_cli.py
More file actions
51 lines (38 loc) · 1.44 KB
/
speedtest_cli.py
File metadata and controls
51 lines (38 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/env python3
"""Simple CLI Internet speed test using rich for output."""
import sys
try:
from rich.console import Console
from rich.table import Table
from rich.progress import Progress
import speedtest
except ImportError as exc:
sys.stderr.write(
"Missing dependencies. Install with 'pip install speedtest-cli rich'\n"
)
raise
def run_speedtest():
console = Console()
st = speedtest.Speedtest()
with Progress(console=console, transient=True) as progress:
task = progress.add_task("Selecting server...", total=3)
st.get_servers()
progress.advance(task)
st.get_best_server()
progress.update(task, description="Testing download speed...")
st.download()
progress.advance(task)
progress.update(task, description="Testing upload speed...")
st.upload()
progress.advance(task)
results = st.results.dict()
table = Table(title="Speed Test Results", show_header=True, header_style="bold magenta")
table.add_column("Metric", style="dim")
table.add_column("Value", justify="right")
table.add_row("Ping", f"{results['ping']:.2f} ms")
table.add_row("Download", f"{results['download'] / 1_000_000:.2f} Mbps")
table.add_row("Upload", f"{results['upload'] / 1_000_000:.2f} Mbps")
table.add_row("Server", results['server']['sponsor'])
console.print(table)
if __name__ == "__main__":
run_speedtest()