您的位置:首页 > 编程语言 > Python开发

python通过wmi获取windows下进程的信息

2015-07-07 11:39 1646 查看
linux应该有很多方法可以获取进程的cpu和内存信息,但windows貌似之前接触的是psutil,后来查了一些资料发现wmi也能够获取进程的信息,但貌似效率不太高,应该可以做监控等性能要求不太高的情况

下载wmi,这个网上很多方法和途径,我是用easyinstall来安装,这个不详细说明了

直接附上代码:

import wmi
from win32com.client import GetObject
import win32gui,time
mywmi = GetObject("winmgmts:")
# allProcess = mywmi.ExecQuery("select * from Win32_Process")
# for i in allProcess:
#     pid = i.Properties_("ProcessID")
#     print pid

# network = mywmi.ExecQuery("select Processor, _Total, Processor Time from PerformanceCounter")
# print network
# for i in network:
#     print i.Properties_("Processor")

mywql= mywmi.ExecQuery("SELECT * FROM Win32_PerfFormattedData_PerfProc_Process where PercentPrivilegedTime>10")

def getPrcessInfo(wql):
while 1:

for j in wql:
#print j.Properties_("PercentPrivilegedTime").__int__()
##print j.Properties_("name").__str__()+" "+j.Properties_("IDProcess").__str__()+"  "+j.Properties_("PercentPrivilegedTime").__str__()
if j.Properties_("name").__str__()!= "_Total" and j.Properties_("name").__str__()!="Idle":
print j.Properties_("name")
print j.Properties_("PercentPrivilegedTime").__int__()
print j.Properties_("WorkingSet").__int__()
time.sleep(1)
#return 1
#break

##print ":)"

getProcessInfo(mysql)


  编码方式停留在初学的基础上,后续需要改进,j.Properties_("属性")得出的是一个实例,所以需要转换成相应的格式;

详细的属性可以参考https://msdn.microsoft.com/en-us/library/aa394277(VS.85).aspx

个人感觉wmi功能挺强大的,能搞通的话,应该可以玩转windows(有点夸张了)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: