import wmi
IP = "192.168.0.1"
username = r".\administrator"
password = r"*********"
# for cls in wmi.WMI().classes:
# print(cls)
wmiServer = wmi.WMI(IP, user=username, password=password)
# MemInfo
total_mem = wmiServer.Win32_ComputerSystem()[0].TotalPhysicalMemory
free_mem = wmiServer.Win32_OperatingSystem()[0].FreePhysicalMemory
print('total mem:', int(total_mem) / 1024 / 1024 / 1024, 'GB')
print('free_mem:', int(free_mem) / 1024 / 1024, 'GB')
# cpuInfo
CPU_data = []
for cpu in wmiServer.Win32_Processor():
# print('Utilization: %s: %d %%' % (cpu.DeviceID, cpu.LoadPercentage))
TmpDict = {}
TmpDict["CPU"] = cpu.DeviceID
TmpDict["CPU状态"] = cpu.CpuStatus
TmpDict["CPU最大速度(MHz)"] = cpu.MaxClockSpeed
TmpDict["CPU当前速度(MHz)"] = cpu.CurrentClockSpeed
TmpDict["CPU使用率"] = cpu.LoadPercentage
CPU_data.append(TmpDict)
print(CPU_data)
# 磁盘空间
GB = 1024 ** 3
Disk_data = []
for Physical_Disk in wmiServer.Win32_DiskDrive():
for Partition in Physical_Disk.associators("Win32_DiskDriveToDiskPartition"):
for Logical_Disk in Partition.associators("Win32_LogicalDiskToPartition"):
TmpDict = {}
TmpDict["盘符"] = Logical_Disk.Caption
TmpDict["总量"] = format(int(Logical_Disk.Size) / GB, '.2f')
TmpDict["使用量"] = format((int(Logical_Disk.Size) - int(Logical_Disk.FreeSpace)) / GB, '.2f')
TmpDict["空闲量"] = format(int(Logical_Disk.FreeSpace) / GB, '.2f')
TmpDict["使用率"] = format(int(100.0 * (int(Logical_Disk.Size) - int(Logical_Disk.FreeSpace)) / int(Logical_Disk.Size)), '.2f') + "%"
Disk_data.append(TmpDict)
print(Disk_data)
笔记:Python 检查windows的CPU 内存 磁盘空间
于 2024-07-02 13:56:49 首次发布