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