python 调用WMI的关键问题---"命名空间"变量!

首先我们看wmi.py中的一段解释内容:The :class:`_wmi_namespace` object itself will determine its classes  and allow you to return all instances of any of them by  using its name as an attribute解释过来是:这个类"命名空间"对象它自己能够决定哪个种类和成功返回值在全部的使用命名空间属性的实例上.那么我们使用以下的语句实例化wmi类就应该是正确的:

                      import  wmi

                      c = wmi.WMI()

                      y = c.Win32_Process()

                     for line in y:

                           print (line)

运行以上程序后,我们返回结果如下:

instance of Win32_Process
{
 Caption = "System Idle Process";
 CreationClassName = "Win32_Process";
 CSCreationClassName = "Win32_ComputerSystem";
 CSName = "BUB";
 Description = "System Idle Process";
 Handle = "0";
 HandleCount = 0;
 KernelModeTime = "385262187500";
 Name = "System Idle Process";
 OSCreationClassName = "Win32_OperatingSystem";
 OSName = "Microsoft Windows XP Professional|C:\\WINDOWS|\\Device\\Harddisk0\\Partition1";
 OtherOperationCount = "0";
 OtherTransferCount = "0";
 PageFaults = 0;
 PageFileUsage = 0;
 ParentProcessId = 0;
 PeakPageFileUsage = 0;
 PeakVirtualSize = "0";
 PeakWorkingSetSize = 0;
 Priority = 0;
 PrivatePageCount = "0";
 ProcessId = 0;
 QuotaNonPagedPoolUsage = 0;
 QuotaPagedPoolUsage = 0;
 QuotaPeakNonPagedPoolUsage = 0;
 QuotaPeakPagedPoolUsage = 0;
 ReadOperationCount = "0";
 ReadTransferCount = "0";
 SessionId = 0;
 ThreadCount = 4;
 UserModeTime = "0";
 VirtualSize = "0";
 WindowsVersion = "5.1.2600";
 WorkingSetSize = "28672";
 WriteOperationCount = "0";
 WriteTransferCount = "0";
};

如上很多信息,每个在任务管理器中运行的程序都能够显示在我们的PYthon回显窗口中,经过修改程序的显示属性,我们能够继续展示如下:

import  wmi

                      c = wmi.WMI()

                      y = c.Win32_Process()

                     for line in y:

                           print (line.Caption,'____',line.Name,line.Handle,'___',line.ProcessId)

具体显示内容如下所示:

System Idle Process ____ System Idle Process 0 ___ 0
System ____ System 4 ___ 4
smss.exe ____ smss.exe 728 ___ 728
csrss.exe ____ csrss.exe 784 ___ 784
winlogon.exe ____ winlogon.exe 828 ___ 828
services.exe ____ services.exe 872 ___ 872
lsass.exe ____ lsass.exe 884 ___ 884
ati2evxx.exe ____ ati2evxx.exe 1060 ___ 1060
svchost.exe ____ svchost.exe 1080 ___ 1080
svchost.exe ____ svchost.exe 1168 ___ 1168
svchost.exe ____ svchost.exe 1268 ___ 1268
EvtEng.exe ____ EvtEng.exe 1352 ___ 1352
S24EvMon.exe ____ S24EvMon.exe 1384 ___ 1384
WLKEEPER.exe ____ WLKEEPER.exe 1436 ___ 1436
svchost.exe ____ svchost.exe 1536 ___ 1536
ati2evxx.exe ____ ati2evxx.exe 1648 ___ 1648
svchost.exe ____ svchost.exe 1800 ___ 1800
spoolsv.exe ____ spoolsv.exe 2028 ___ 2028

截取内容如上,好象都可以成功实例化,如果我们使用另外一个命名空间的类,看看python怎么识别

在'root\WMI'中有一个类叫做BatteryRuntime,我们同样使用以下语句实例化wmi,看看情况

import  wmi

                      c = wmi.WMI()

                      y = c.BatteryRuntime()

提示是这样的:

Traceback (most recent call last):
  File "<pyshell#21>", line 1, in <module>
    y = c.BatteryRuntime()
  File "C:\Python33\lib\wmi.py", line 1147, in __getattr__
    return getattr (self._namespace, attribute)
  File "C:\Python33\lib\site-packages\win32com\client\dynamic.py", line 522, in __getattr__
    raise AttributeError("%s.%s" % (self._username_, attr))
AttributeError: winmgmts:.BatteryRuntime

因此我们使用变量查看语句,直接在不同的命名空间中输入显示结果如下所示:

Python 3.3.0 (v3.3.0:bd8afb90ebf2, Sep 29 2012, 10:55:48) [MSC v.1600 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> import wmi
>>> c = wmi.WMI()
>>> c
<_wmi_namespace: <COMObject winmgmts:>>
>>> y = c.BatteryRuntime()
Traceback (most recent call last):
  File "<pyshell#3>", line 1, in <module>
    y = c.BatteryRuntime()
  File "C:\Python33\lib\wmi.py", line 1147, in __getattr__
    return getattr (self._namespace, attribute)
  File "C:\Python33\lib\site-packages\win32com\client\dynamic.py", line 522, in __getattr__
    raise AttributeError("%s.%s" % (self._username_, attr))
AttributeError: winmgmts:.BatteryRuntime
>>>

然后使用另外一种命名空间后,我们能够看到

Python 3.3.0 (v3.3.0:bd8afb90ebf2, Sep 29 2012, 10:55:48) [MSC v.1600 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> import wmi
>>> c = wmi.WMI(namespace='root/WMI')
>>> c
<_wmi_namespace: <COMObject winmgmts:root/WMI>>
>>> y = c.BatteryRuntime()
>>> y
[<_wmi_object: b'\\\\BUB\\root\\WMI:BatteryRuntime.InstanceName="ACPI\\\\PNP0C0A\\\\1_0"'>]
>>>

显示成功输出WMI类对象.

因此在我们使用WMI实例化时,请大家尽量明确"命名空间"这个参数,不然会带来意想不到的错误.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值