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


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(有点夸张了)


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM