简体   繁体   English

如何在python中访问wmi?

[英]How to access wmi in python?

So I am trying to access the data from here所以我试图从这里访问数据

in Python.在 Python 中。 As you can see, it uses wmi.如您所见,它使用 wmi。 I have tried to use wmi in python before but I am having trouble interpreting the data they are giving me.我之前曾尝试在 python 中使用 wmi,但我无法解释他们提供给我的数据。 Please be patient with me as I am a noob to how wmi works.请耐心等待,因为我不了解 wmi 的工作原理。 It says that the wmi data is stored in root/OpenHardwareMontor and that it uses two different wmi classes(Hardware and Sensor).它说 wmi 数据存储在 root/OpenHardwareMontor 中,并且它使用两个不同的 wmi 类(硬件和传感器)。 But all this information is going over my head.但所有这些信息都在我的脑海里。

could someone please give me some sample code to read some data from this?有人可以给我一些示例代码来从中读取一些数据吗?

For example, the code to check cpu core 1 frequency.例如,检查cpu core 1频率的代码。

EDIT: i have sort of got it working.编辑:我有点让它工作。 i run this code:我运行这个代码:

for Temperature in c.sensor():
    print Temperature.identifier
    print Temperature.value

and i get this:我明白了:

/hdd/0/load/0
37.6608924866
/intelcpu/0/temperature/1
53.0
/intelcpu/0/temperature/0
42.0
/ram/data/1
2.88324356079
/intelcpu/0/load/2
1.53846144676
/hdd/0/temperature/0
43.0
/intelcpu/0/load/0
2.30768918991
/intelcpu/0/clock/1
1463.29663086
/intelcpu/0/clock/0
133.02696228
/intelcpu/0/clock/2
1463.29663086
/ram/load/0
49.224521637
/ram/data/0
2.79517364502
/intelcpu/0/load/1
3.07692289352

how can i request only the value associated with the identifier /intelcpu/0/temperature/1 ignoring all other values?如何请求与标识符 /intelcpu/0/temperature/1 相关的值而忽略所有其他值?

The most simple example to use WMI:使用 WMI 的最简单示例:

c = wmi.WMI()
wql = "Select * From Win32_SerialPort"
for item in c.query(wql):
    print item

Output Example:输出示例:

instance of Win32_SerialPort
{
    Availability = 2;
    Binary = TRUE;
    Caption = "SpectrumAnalyzer1 (COM15)";
    ConfigManagerErrorCode = 0;
    ConfigManagerUserConfig = FALSE;
    CreationClassName = "Win32_SerialPort";
    Description = "SpectrumAnalyzer1";
    DeviceID = "COM15";
    MaxBaudRate = 128000;
    MaximumInputBufferSize = 0;
    MaximumOutputBufferSize = 0;
    Name = "SpectrumAnalyzer1 (COM15)";
    OSAutoDiscovered = TRUE;
    PNPDeviceID = "USB\\VID_10C4&PID_ED00\\1269376";
    PowerManagementCapabilities = {1};
    PowerManagementSupported = FALSE;
    ProviderType = "RS232 Serial Port";
    SettableBaudRate = TRUE;
    SettableDataBits = TRUE;
    SettableFlowControl = TRUE;
    SettableParity = TRUE;
    SettableParityCheck = TRUE;
    SettableRLSD = TRUE;
    SettableStopBits = TRUE;
    Status = "OK";
    StatusInfo = 3;
    Supports16BitMode = FALSE;
    SupportsDTRDSR = TRUE;
    SupportsElapsedTimeouts = TRUE;
    SupportsIntTimeouts = TRUE;
    SupportsParityCheck = TRUE;
    SupportsRLSD = TRUE;
    SupportsRTSCTS = TRUE;
    SupportsSpecialCharacters = TRUE;
    SupportsXOnXOff = TRUE;
    SupportsXOnXOffSet = TRUE;
    SystemCreationClassName = "Win32_ComputerSystem";
    SystemName = ".......";
};

You can access each item by:您可以通过以下方式访问每个item

myQuery = c.query(wql)
myQuery.Availability 

Output:输出:

2

For more information, try the WMI cookbook .有关更多信息,请尝试 WMI说明书

Edit #1:编辑#1:

Using if statements and in you can do what you want.使用 if 语句和in你可以做你想做的。

for Temperature in c.sensor():
    if "/intelcpu/0/temperature/1" in Temperature.identifier:
        print Temperature.identifier
        print Temperature.value 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM