Two examples of using Python to get information about windowns such as CPU memory and hard disk
- 2020-04-02 13:33:17
- OfStack
Example 1:
Python USES the WMI module to obtain windowns hardware information: hard disk partition, usage, memory size, CPU model, current running process, bootup program and location, system version and so on.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import wmi
import os
import sys
import platform
import time
def sys_version():
c = wmi.WMI ()
# Gets the operating system version
for sys in c.Win32_OperatingSystem():
print "Version:%s" % sys.Caption.encode("UTF8"),"Vernum:%s" % sys.BuildNumber
print sys.OSArchitecture.encode("UTF8")# System is 32 A still 64 bit
print sys.NumberOfProcesses # Total number of processes currently running on the system
def cpu_mem():
c = wmi.WMI ()
#CPU Type and memory
for processor in c.Win32_Processor():
#print "Processor ID: %s" % processor.DeviceID
print "Process Name: %s" % processor.Name.strip()
for Memory in c.Win32_PhysicalMemory():
print "Memory Capacity: %.fMB" %(int(Memory.Capacity)/1048576)
def cpu_use():
#5s Take a CPU The use of
c = wmi.WMI()
while True:
for cpu in c.Win32_Processor():
timestamp = time.strftime('%a, %d %b %Y %H:%M:%S', time.localtime())
print '%s | Utilization: %s: %d %%' % (timestamp, cpu.DeviceID, cpu.LoadPercentage)
time.sleep(5)
def disk():
c = wmi.WMI ()
# Get hard disk partition
for physical_disk in c.Win32_DiskDrive ():
for partition in physical_disk.associators ("Win32_DiskDriveToDiskPartition"):
for logical_disk in partition.associators ("Win32_LogicalDiskToPartition"):
print physical_disk.Caption.encode("UTF8"), partition.Caption.encode("UTF8"), logical_disk.Caption
# Gets the percentage of disk usage
for disk in c.Win32_LogicalDisk (DriveType=3):
print disk.Caption, "%0.2f%% free" % (100.0 * long (disk.FreeSpace) / long (disk.Size))
def network():
c = wmi.WMI ()
# To obtain MAC and IP address
for interface in c.Win32_NetworkAdapterConfiguration (IPEnabled=1):
print "MAC: %s" % interface.MACAddress
for ip_address in interface.IPAddress:
print "ip_add: %s" % ip_address
print
# Gets the location of the bootstrap program
for s in c.Win32_StartupCommand ():
print "[%s] %s <%s>" % (s.Location.encode("UTF8"), s.Caption.encode("UTF8"), s.Command.encode("UTF8"))
# Gets the currently running process
for process in c.Win32_Process ():
print process.ProcessId, process.Name
def main():
sys_version()
#cpu_mem()
#disk()
#network()
#cpu_use()
if __name__ == '__main__':
main()
print platform.system()
print platform.release()
print platform.version()
print platform.platform()
print platform.machine()
Example 2:
Since I don't use much, I only get the CPU, memory and hard disk. If you need other resources, please refer to MSDN.
import os
import win32api
import win32con
import wmi
import time
def getSysInfo(wmiService = None):
result = {}
if wmiService == None:
wmiService = wmi.WMI()
# cpu
for cpu in wmiService.Win32_Processor():
timestamp = time.strftime('%a, %d %b %Y %H:%M:%S', time.localtime())
result['cpuPercent'] = cpu.loadPercentage
# memory
cs = wmiService.Win32_ComputerSystem()
os = wmiService.Win32_OperatingSystem()
result['memTotal'] = int(int(cs[0].TotalPhysicalMemory)/1024/1024)
result['memFree'] = int(int(os[0].FreePhysicalMemory)/1024)
#disk
result['diskTotal'] = 0
result['diskFree'] = 0
for disk in wmiService.Win32_LogicalDisk(DriveType=3):
result['diskTotal'] += int(disk.Size)
result['diskFree'] += int(disk.FreeSpace)
result['diskTotal'] = int(result['diskTotal']/1024/1024)
result['diskFree'] = int(result['diskFree']/1024/1024)
return result
if __name__ == '__main__':
wmiService = wmi.WMI()
while True:
print getSysInfo(wmiService)
time.sleep(3)
Adopted by the wmi module, because wmi initialization takes up too much system resources, so if you need to loop to get, please initialize the wmi object outside the loop body, and then into the function, so that the CPU will not be too high.