使用Python获取CPU、内存和硬盘等windowns系统信息的2个例子
例子一:
python用wmi模块获取windowns系统的硬件信息:硬盘分区、使用情况,内存大小,cpu型号,当前运行的进程,自启动程序及位置,系统的版本等信息。
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import wmi
import os
import sys
import platform
import time
def sys_version():
c = wmi.wmi ()
#获取操作系统版本
for sys in c.win32_operatingsystem():
print "version:%s" % sys.caption.encode("utf8"),"vernum:%s" % sys.buildnumber
print sys.osarchitecture.encode("utf8")#系统是32位还是64位的
print sys.numberofprocesses #当前系统运行的进程总数
def cpu_mem():
c = wmi.wmi ()
#cpu类型和内存
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取一次cpu的使用率
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 ()
#获取硬盘分区
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
#获取硬盘使用百分情况
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 ()
#获取mac和ip地址
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
#获取自启动程序的位置
for s in c.win32_startupcommand ():
print "[%s] %s <%s>" % (s.location.encode("utf8"), s.caption.encode("utf8"), s.command.encode("utf8"))
#获取当前运行的进程
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()
例子二:
由于我用到的不多,所以只获取的cpu、内存和硬盘,如果需要其它资源,请参照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)
采用的wmi模块获取的,由于wmi初始化时占用系统资源太高,所以如果需要循环获取,请在循环体外面把wmi对象初始化好,然后传入函数里面,这样就不会产生cpu资源过高的情况。