gpt4 book ai didi

python - 如何在 python 中访问 wmi?

转载 作者:可可西里 更新时间:2023-11-01 13:25:02 25 4
gpt4 key购买 nike

所以我正在尝试访问来自 here 的数据

在 Python 中。如您所见,它使用 wmi。我之前曾尝试在 python 中使用 wmi,但我无法解释他们给我的数据。请耐心等待我,因为我对 wmi 的工作原理一窍不通。它说 wmi 数据存储在 root/OpenHardwareMontor 中,并且它使用两个不同的 wmi 类(硬件和传感器)。但所有这些信息都在我脑海中浮现。

有人可以给我一些示例代码来从中读取一些数据吗?

例如检查cpu core 1频率的代码。

编辑:我有点让它工作了。我运行这段代码:

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

我明白了:

/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

我如何请求与标识符/intelcpu/0/temperature/1 关联的值而忽略所有其他值?

最佳答案

使用WMI最简单的例子:

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

输出示例:

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 = ".......";
};

您可以通过以下方式访问每个项目:

myQuery = c.query(wql)
myQuery.Availability

输出:

2

有关详细信息,请尝试 WMI cookbook .

编辑#1:

使用 if 语句和 in 你可以做你想做的事。

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

关于python - 如何在 python 中访问 wmi?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21075471/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com