Spaces:
Sleeping
Sleeping
| import psutil | |
| import platform | |
| import socket | |
| import uuid | |
| def get_cpu_info(): | |
| """获取CPU信息""" | |
| cpu_count_logical = psutil.cpu_count(logical=True) | |
| cpu_count_physical = psutil.cpu_count(logical=False) | |
| cpu_percent = psutil.cpu_percent(interval=1) | |
| try: | |
| import cpuinfo | |
| brand = cpuinfo.get_cpu_info().get('brand_raw', 'Unknown') | |
| except ImportError: | |
| brand = platform.processor() | |
| return { | |
| "物理核心数": cpu_count_physical, | |
| "逻辑核心数": cpu_count_logical, | |
| "当前使用率": f"{cpu_percent}%", | |
| "处理器型号": brand | |
| } | |
| def get_memory_info(): | |
| """获取内存信息""" | |
| mem = psutil.virtual_memory() | |
| swap = psutil.swap_memory() | |
| return { | |
| "总内存(GB)": round(mem.total / (1024**3), 2), | |
| "已用内存(GB)": round(mem.used / (1024**3), 2), | |
| "可用内存(GB)": round(mem.available / (1024**3), 2), | |
| "内存使用率": f"{mem.percent}%", | |
| "交换分区总大小(GB)": round(swap.total / (1024**3), 2), | |
| "交换分区使用率": f"{swap.percent}%" | |
| } | |
| def get_disk_info(): | |
| """获取磁盘信息""" | |
| partitions = psutil.disk_partitions() | |
| disk_info = [] | |
| for partition in partitions: | |
| try: | |
| usage = psutil.disk_usage(partition.mountpoint) | |
| disk_info.append({ | |
| "设备": partition.device, | |
| "挂载点": partition.mountpoint, | |
| "文件系统": partition.fstype, | |
| "总大小(GB)": round(usage.total / (1024**3), 2), | |
| "已用(GB)": round(usage.used / (1024**3), 2), | |
| "空闲(GB)": round(usage.free / (1024**3), 2), | |
| "使用率": f"{usage.percent}%" | |
| }) | |
| except PermissionError: | |
| continue | |
| return disk_info | |
| def get_gpu_info(): | |
| """获取显卡信息""" | |
| gpu_info = [] | |
| # 尝试使用 GPUtil (NVIDIA) | |
| try: | |
| import GPUtil | |
| gpus = GPUtil.getGPUs() | |
| for gpu in gpus: | |
| gpu_info.append({ | |
| "ID": gpu.id, | |
| "名称": gpu.name, | |
| "显存总大小(MB)": gpu.memoryTotal, | |
| "显存已用(MB)": gpu.memoryUsed, | |
| "显存使用率": f"{gpu.memoryUtil*100}%", | |
| "GPU使用率": f"{gpu.load*100}%", | |
| "温度": f"{gpu.temperature}°C" | |
| }) | |
| except ImportError: | |
| pass | |
| # 如果 GPUtil 不可用或未检测到,尝试 pynvml | |
| if not gpu_info: | |
| try: | |
| import pynvml | |
| pynvml.nvmlInit() | |
| device_count = pynvml.nvmlDeviceGetCount() | |
| for i in range(device_count): | |
| handle = pynvml.nvmlDeviceGetHandleByIndex(i) | |
| name = pynvml.nvmlDeviceGetName(handle) | |
| mem_info = pynvml.nvmlDeviceGetMemoryInfo(handle) | |
| temp = pynvml.nvmlDeviceGetTemperature(handle, 0) | |
| utilization = pynvml.nvmlDeviceGetUtilizationRates(handle) | |
| if isinstance(name, bytes): | |
| name = name.decode('utf-8') | |
| gpu_info.append({ | |
| "索引": i, | |
| "名称": name, | |
| "显存总大小(MB)": mem_info.total // (1024**2), | |
| "显存已用(MB)": mem_info.used // (1024**2), | |
| "GPU使用率": f"{utilization.gpu}%", | |
| "显存使用率": f"{utilization.memory}%", | |
| "温度": f"{temp}°C" | |
| }) | |
| pynvml.nvmlShutdown() | |
| except Exception: | |
| pass | |
| if not gpu_info: | |
| return [{"提示": "未检测到NVIDIA显卡或未安装GPUtil/pynvml库"}] | |
| return gpu_info | |
| def get_network_info(): | |
| """获取网络IP和MAC信息""" | |
| net_info = {} | |
| # 获取所有网络接口的地址 | |
| addrs = psutil.net_if_addrs() | |
| for interface, addr_list in addrs.items(): | |
| ipv4 = None | |
| mac = None | |
| for addr in addr_list: | |
| if addr.family == socket.AF_INET: | |
| ipv4 = addr.address | |
| elif addr.family == psutil.AF_LINK: # MAC address | |
| mac = addr.address | |
| if ipv4 or mac: | |
| net_info[interface] = { | |
| "IPv4": ipv4, | |
| "MAC": mac | |
| } | |
| # 获取默认上网IP | |
| try: | |
| s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) | |
| s.connect(("8.8.8.8", 80)) | |
| default_ip = s.getsockname() | |
| s.close() | |
| except Exception: | |
| default_ip = "127.0.0.1" | |
| return { | |
| "默认上网IP": default_ip, | |
| "所有接口详情": net_info | |
| } | |
| if __name__ == "__main__": | |
| print("="*30 + " CPU信息 " + "="*30) | |
| cpu_data = get_cpu_info() | |
| for k, v in cpu_data.items(): | |
| print(f"{k}: {v}") | |
| print("\n" + "="*30 + " 内存信息 " + "="*30) | |
| mem_data = get_memory_info() | |
| for k, v in mem_data.items(): | |
| print(f"{k}: {v}") | |
| print("\n" + "="*30 + " 磁盘信息 " + "="*30) | |
| disk_data = get_disk_info() | |
| for disk in disk_data: | |
| for k, v in disk.items(): | |
| print(f"{k}: {v}") | |
| print("-" * 20) | |
| print("\n" + "="*30 + " 显卡信息 " + "="*30) | |
| gpu_data = get_gpu_info() | |
| for gpu in gpu_data: | |
| for k, v in gpu.items(): | |
| print(f"{k}: {v}") | |
| print("-" * 20) | |
| print("\n" + "="*30 + " 网络信息 " + "="*30) | |
| net_data = get_network_info() | |
| print(f"默认上网IP: {net_data['默认上网IP']}") | |
| print("接口详情:") | |
| for iface, info in net_data['所有接口详情'].items(): | |
| print(f" 接口: {iface}, IPv4: {info['IPv4']}, MAC: {info['MAC']}") | |