| 1 |
a47eb4c |
Dawsh |
2y ago |
import math |
| 2 |
a47eb4c |
Dawsh |
2y ago |
import secrets |
| 3 |
a47eb4c |
Dawsh |
2y ago |
import socket |
| 4 |
a47eb4c |
Dawsh |
2y ago |
from dataclasses import dataclass |
| 5 |
a47eb4c |
Dawsh |
2y ago |
|
| 6 |
a47eb4c |
Dawsh |
2y ago |
import psutil |
| 7 |
a47eb4c |
Dawsh |
2y ago |
|
| 8 |
a47eb4c |
Dawsh |
2y ago |
|
| 9 |
a47eb4c |
Dawsh |
2y ago |
@dataclass |
| 10 |
f7af2b3 |
Dawsh |
2y ago |
class MemoryStat: |
| 11 |
a47eb4c |
Dawsh |
2y ago |
total: int |
| 12 |
a47eb4c |
Dawsh |
2y ago |
used: int |
| 13 |
a47eb4c |
Dawsh |
2y ago |
free: int |
| 14 |
a47eb4c |
Dawsh |
2y ago |
|
| 15 |
a47eb4c |
Dawsh |
2y ago |
|
| 16 |
a47eb4c |
Dawsh |
2y ago |
@dataclass |
| 17 |
f7af2b3 |
Dawsh |
2y ago |
class CPUStat: |
| 18 |
a47eb4c |
Dawsh |
2y ago |
cores: int |
| 19 |
a47eb4c |
Dawsh |
2y ago |
percent: float |
| 20 |
a47eb4c |
Dawsh |
2y ago |
|
| 21 |
a47eb4c |
Dawsh |
2y ago |
|
| 22 |
a47eb4c |
Dawsh |
2y ago |
def cpu_usage() -> CPUStat: |
| 23 |
a47eb4c |
Dawsh |
2y ago |
return CPUStat(cores=psutil.cpu_count(), percent=psutil.cpu_percent()) |
| 24 |
a47eb4c |
Dawsh |
2y ago |
|
| 25 |
a47eb4c |
Dawsh |
2y ago |
|
| 26 |
a47eb4c |
Dawsh |
2y ago |
def memory_usage() -> MemoryStat: |
| 27 |
a47eb4c |
Dawsh |
2y ago |
mem = psutil.virtual_memory() |
| 28 |
a47eb4c |
Dawsh |
2y ago |
return MemoryStat(total=mem.total, used=mem.used, free=mem.available) |
| 29 |
a47eb4c |
Dawsh |
2y ago |
|
| 30 |
a47eb4c |
Dawsh |
2y ago |
|
| 31 |
a47eb4c |
Dawsh |
2y ago |
@dataclass |
| 32 |
a47eb4c |
Dawsh |
2y ago |
class RealtimeBandwidth: |
| 33 |
a47eb4c |
Dawsh |
2y ago |
def __post_init__(self): |
| 34 |
a47eb4c |
Dawsh |
2y ago |
io = psutil.net_io_counters() |
| 35 |
a47eb4c |
Dawsh |
2y ago |
self.bytes_recv = io.bytes_recv |
| 36 |
a47eb4c |
Dawsh |
2y ago |
self.bytes_sent = io.bytes_sent |
| 37 |
a47eb4c |
Dawsh |
2y ago |
self.packets_recv = io.packets_recv |
| 38 |
a47eb4c |
Dawsh |
2y ago |
self.packet_sent = io.packets_sent |
| 39 |
a47eb4c |
Dawsh |
2y ago |
|
| 40 |
a47eb4c |
Dawsh |
2y ago |
incoming_bytes: int |
| 41 |
a47eb4c |
Dawsh |
2y ago |
outgoing_bytes: int |
| 42 |
a47eb4c |
Dawsh |
2y ago |
incoming_packets: int |
| 43 |
a47eb4c |
Dawsh |
2y ago |
outgoing_packets: int |
| 44 |
a47eb4c |
Dawsh |
2y ago |
|
| 45 |
a47eb4c |
Dawsh |
2y ago |
bytes_recv: int = None |
| 46 |
a47eb4c |
Dawsh |
2y ago |
bytes_sent: int = None |
| 47 |
a47eb4c |
Dawsh |
2y ago |
packets_recv: int = None |
| 48 |
a47eb4c |
Dawsh |
2y ago |
packet_sent: int = None |
| 49 |
a47eb4c |
Dawsh |
2y ago |
|
| 50 |
a47eb4c |
Dawsh |
2y ago |
|
| 51 |
a47eb4c |
Dawsh |
2y ago |
@dataclass |
| 52 |
a47eb4c |
Dawsh |
2y ago |
class RealtimeBandwidthStat: |
| 53 |
a47eb4c |
Dawsh |
2y ago |
incoming_bytes: int |
| 54 |
a47eb4c |
Dawsh |
2y ago |
outgoing_bytes: int |
| 55 |
a47eb4c |
Dawsh |
2y ago |
incoming_packets: int |
| 56 |
a47eb4c |
Dawsh |
2y ago |
outgoing_packets: int |
| 57 |
a47eb4c |
Dawsh |
2y ago |
|
| 58 |
a47eb4c |
Dawsh |
2y ago |
|
| 59 |
a47eb4c |
Dawsh |
2y ago |
rt_bw = RealtimeBandwidth( |
| 60 |
a47eb4c |
Dawsh |
2y ago |
incoming_bytes=0, outgoing_bytes=0, incoming_packets=0, outgoing_packets=0) |
| 61 |
a47eb4c |
Dawsh |
2y ago |
|
| 62 |
a47eb4c |
Dawsh |
2y ago |
|
| 63 |
a47eb4c |
Dawsh |
2y ago |
def realtime_bandwidth() -> RealtimeBandwidthStat: |
| 64 |
a47eb4c |
Dawsh |
2y ago |
return RealtimeBandwidthStat( |
| 65 |
a47eb4c |
Dawsh |
2y ago |
incoming_bytes=rt_bw.incoming_bytes, outgoing_bytes=rt_bw.outgoing_bytes, |
| 66 |
a47eb4c |
Dawsh |
2y ago |
incoming_packets=rt_bw.incoming_packets, outgoing_packets=rt_bw.outgoing_packets) |
| 67 |
a47eb4c |
Dawsh |
2y ago |
|
| 68 |
a47eb4c |
Dawsh |
2y ago |
|
| 69 |
a47eb4c |
Dawsh |
2y ago |
def random_password() -> str: |
| 70 |
a47eb4c |
Dawsh |
2y ago |
return secrets.token_urlsafe(16) |
| 71 |
a47eb4c |
Dawsh |
2y ago |
|
| 72 |
a47eb4c |
Dawsh |
2y ago |
|
| 73 |
a47eb4c |
Dawsh |
2y ago |
def check_port(port: int) -> bool: |
| 74 |
a47eb4c |
Dawsh |
2y ago |
s = socket.socket() |
| 75 |
a47eb4c |
Dawsh |
2y ago |
try: |
| 76 |
a47eb4c |
Dawsh |
2y ago |
s.connect(('127.0.0.1', port)) |
| 77 |
a47eb4c |
Dawsh |
2y ago |
return True |
| 78 |
a47eb4c |
Dawsh |
2y ago |
except socket.error: |
| 79 |
a47eb4c |
Dawsh |
2y ago |
return False |
| 80 |
a47eb4c |
Dawsh |
2y ago |
finally: |
| 81 |
a47eb4c |
Dawsh |
2y ago |
s.close() |
| 82 |
a47eb4c |
Dawsh |
2y ago |
|
| 83 |
a47eb4c |
Dawsh |
2y ago |
|
| 84 |
a47eb4c |
Dawsh |
2y ago |
def get_public_ip(): |
| 85 |
a47eb4c |
Dawsh |
2y ago |
try: |
| 86 |
a47eb4c |
Dawsh |
2y ago |
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) |
| 87 |
a47eb4c |
Dawsh |
2y ago |
sock.connect(('8.8.8.8', 80)) |
| 88 |
a47eb4c |
Dawsh |
2y ago |
return sock.getsockname()[0] |
| 89 |
a47eb4c |
Dawsh |
2y ago |
except (socket.error, IndexError): |
| 90 |
a47eb4c |
Dawsh |
2y ago |
pass |
| 91 |
a47eb4c |
Dawsh |
2y ago |
finally: |
| 92 |
a47eb4c |
Dawsh |
2y ago |
sock.close() |
| 93 |
a47eb4c |
Dawsh |
2y ago |
|
| 94 |
a47eb4c |
Dawsh |
2y ago |
return '127.0.0.1' |
| 95 |
a47eb4c |
Dawsh |
2y ago |
|
| 96 |
a47eb4c |
Dawsh |
2y ago |
|
| 97 |
a47eb4c |
Dawsh |
2y ago |
def readable_size(size_bytes): |
| 98 |
a47eb4c |
Dawsh |
2y ago |
if size_bytes == 0: |
| 99 |
a47eb4c |
Dawsh |
2y ago |
return "0 B" |
| 100 |
a47eb4c |
Dawsh |
2y ago |
size_name = ("B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB") |
| 101 |
a47eb4c |
Dawsh |
2y ago |
i = int(math.floor(math.log(size_bytes, 1024))) |
| 102 |
a47eb4c |
Dawsh |
2y ago |
p = math.pow(1024, i) |
| 103 |
a47eb4c |
Dawsh |
2y ago |
s = round(size_bytes / p, 2) |
| 104 |
a47eb4c |
Dawsh |
2y ago |
return f'{s} {size_name[i]}' |