import math import secrets import socket from dataclasses import dataclass import psutil @dataclass class MemoryStat: total: int used: int free: int @dataclass class CPUStat: cores: int percent: float def cpu_usage() -> CPUStat: return CPUStat(cores=psutil.cpu_count(), percent=psutil.cpu_percent()) def memory_usage() -> MemoryStat: mem = psutil.virtual_memory() return MemoryStat(total=mem.total, used=mem.used, free=mem.available) @dataclass class RealtimeBandwidth: def __post_init__(self): io = psutil.net_io_counters() self.bytes_recv = io.bytes_recv self.bytes_sent = io.bytes_sent self.packets_recv = io.packets_recv self.packet_sent = io.packets_sent incoming_bytes: int outgoing_bytes: int incoming_packets: int outgoing_packets: int bytes_recv: int = None bytes_sent: int = None packets_recv: int = None packet_sent: int = None @dataclass class RealtimeBandwidthStat: incoming_bytes: int outgoing_bytes: int incoming_packets: int outgoing_packets: int rt_bw = RealtimeBandwidth( incoming_bytes=0, outgoing_bytes=0, incoming_packets=0, outgoing_packets=0 ) def realtime_bandwidth() -> RealtimeBandwidthStat: return RealtimeBandwidthStat( incoming_bytes=rt_bw.incoming_bytes, outgoing_bytes=rt_bw.outgoing_bytes, incoming_packets=rt_bw.incoming_packets, outgoing_packets=rt_bw.outgoing_packets, ) def random_password() -> str: return secrets.token_urlsafe(16) def check_port(port: int) -> bool: s = socket.socket() try: s.connect(("127.0.0.1", port)) return True except socket.error: return False finally: s.close() def get_public_ip(): try: sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) sock.connect(("8.8.8.8", 80)) return sock.getsockname()[0] except (socket.error, IndexError): pass finally: sock.close() return "127.0.0.1" def readable_size(size_bytes): if size_bytes == 0: return "0 B" size_name = ("B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB") i = int(math.floor(math.log(size_bytes, 1024))) p = math.pow(1024, i) s = round(size_bytes / p, 2) return f"{s} {size_name[i]}"