snoopnetworktest.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #! /usr/bin/env python3
  2. # Copyright (c) 2020 Snoop Project <snoopproject@protonmail.com>
  3. "Самотестирование сети"
  4. import speedtest
  5. from rich.console import Console
  6. from rich.panel import Panel
  7. from rich.style import Style as STL
  8. def nettest():
  9. console2 = Console()
  10. with console2.status("[cyan] Ожидайте, идёт самотестирование сети..."):
  11. servers = []
  12. try:
  13. s = speedtest.Speedtest(secure=True)
  14. s.get_servers(servers)
  15. s.get_best_server()
  16. s.download(threads=None)
  17. s.upload(threads=None)
  18. a = s.results.dict()
  19. d = round(a.get("download") / 1_000_000, 2)
  20. u = round(a.get("upload") / 1_000_000, 2)
  21. p = round(a.get("ping"))
  22. c = a.get("client")
  23. # Скорость загрузки.
  24. try:
  25. if d < 3: d = f"Download: [bold red]{d}[/bold red] Мбит/с"
  26. elif 3 <= d <= 5.5: d = f"Download: [yellow]{d}[/yellow] Мбит/с"
  27. elif d > 5.5: d = f"Download: [bold green]{d}[/bold green] Мбит/с"
  28. except:
  29. d = f"Download: [bold red]Сбой[/bold red]"
  30. # Скорость выгрузки.
  31. try:
  32. if u < 0.8: u = f"Upload: [bold red]{u}[/bold red] Мбит/с"
  33. elif 0.8 <= u <= 1.5: u = f"Upload: [yellow]{u}[/yellow] Мбит/с"
  34. elif u > 1.5: u = f"Upload: [bold green]{u}[/bold green] Мбит/с"
  35. except:
  36. u = f"Upload: [bold red]Сбой[/bold red]"
  37. # Ping.
  38. try:
  39. if p >= 250: p = f"Ping: [bold red]{p}[/bold red] мс"
  40. elif 60 <= p < 250: p = f"Ping: [yellow]{p}[/yellow] мс"
  41. elif p < 60: p = f"Ping: [bold green]{p}[/bold green] мс"
  42. except:
  43. p = f"Ping: [bold red]Сбой[/bold red]"
  44. # Результат.
  45. console2.print(Panel.fit(f"{d}\n{u}\n{p}\n\nВаш ip: {c.get('ip')}\nПровайдер: " + \
  46. f"{c.get('isp')}\nЛокация: {c.get('country')}",
  47. title="🌐 Тест сети", style=STL(color="cyan")))
  48. console2.log("[cyan]--> завершен")
  49. except Exception:
  50. console2.print(f"[bold red]Аномалии в сети.\nТест будет пропущен...")