得益于PyWin32的强大,在Windows下可以通过Python调用Performon COM接口来查看进程的信息。下面的代码就是查看svhost进程的相关信息的示例代码,如果需要其他信息,请自行添加counter

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#/!/usr/bin/env python
# -*- coding: utf-8 -*-

import win32api, win32pdh, win32pdhutil
import time

def ShowAllProcesses():

    procs = []
    object = win32pdhutil.find_pdh_counter_localized_name("Process")

    items, instances = win32pdh.EnumObjectItems(None,None,object,win32pdh.PERF_DETAIL_WIZARD)
    instance_dict = {}

    for instance in instances:
        try:
            if instance == 'svchost':
                instance_dict[instance] = instance_dict[instance] + 1
        except KeyError:
            instance_dict[instance] = 0

    items = [win32pdhutil.find_pdh_counter_localized_name("ID Process")] + items[0:]

    for instance, max_instances in instance_dict.items():

        for inum in xrange(max_instances+1):

            hq = win32pdh.OpenQuery()

            hcs = []
            for item in items:
                path = win32pdh.MakeCounterPath((None,object, instance,None, inum, item))
                hcs.append(win32pdh.AddCounter(hq, path))

            win32pdh.CollectQueryData(hq)

            time.sleep(0.01)
            win32pdh.CollectQueryData(hq)
            proc = instance[:15]
            hc = hcs[0]

            vals = []
            for i in range(len(hcs)):
                hc = hcs[i]
                type, val = win32pdh.GetFormattedCounterValue(hc, win32pdh.PDH_FMT_LONG)
                vals.append(val)

            win32pdh.RemoveCounter(hc)

            procs.append([proc, [vals] ])
            win32pdh.CloseQuery(hq)

    print procs
    return procs
ShowAllProcesses()