python中的坑(1)- 獲取CPU、主板、內存、硬盤、網卡等硬件信息

在windows系統環境中,python通過wmi模塊獲取系統各設備信息,wmi,英文全稱:Windows Management Instrumentation,是一項核心的Windows管理技術,wmi作為一種規範和基礎結構,通過它可以訪問、配置、管理和監視幾乎所有的Windows資源。

★★★wmi模塊依賴pywin32模塊,在安裝wmi之前,先安裝pywin32★★★

(1)通過下載安裝包,手動安裝pywin32,pywin32的官方網址是https://github.com/mhammond/pywin32/releases,一定要下載與自己的python版本一致的安裝包

(2)使用pip命令安裝wmi:pip install wmi

在pycharm中新建python文件,錄入一下代碼:

#!/usr/bin/env python# -*- coding: utf-8 -*-import osimport sysimport wmic = wmi.WMI()# 處理器def get_CPU_Info():    tmpdict = {}    tmpdict["CpuCores"] = 0    for cpu in c.Win32_Processor():        tmpdict["cpuid"] = cpu.ProcessorId.strip()        tmpdict["CpuType"] = cpu.Name        tmpdict['systemName'] = cpu.SystemName        try:            tmpdict["CpuCores"] = cpu.NumberOfCores        except:            tmpdict["CpuCores"] += 1        tmpdict["CpuClock"] = cpu.MaxClockSpeed        tmpdict['DataWidth'] = cpu.DataWidth    return tmpdict# 主板def get_Mainboard_Info():    boards = []    # print len(c.Win32_BaseBoard()):    for board_id in c.Win32_BaseBoard():        tmpmsg = {}        tmpmsg['UUID'] = board_id.qualifiers['UUID'][1:-1]  # 主板UUID,有的主板這部分信息取到為空值,ffffff-ffffff這樣的        tmpmsg['SerialNumber'] = board_id.SerialNumber  # 主板序列號        tmpmsg['Manufacturer'] = board_id.Manufacturer  # 主板生產品牌廠家        tmpmsg['Product'] = board_id.Product  # 主板型號        boards.append(tmpmsg)    return boards# BIOSdef get_BIOS_Info():    bioss = []    for bios_id in c.Win32_BIOS():        tmpmsg = {}        tmpmsg['BiosCharacteristics'] = bios_id.BiosCharacteristics  # BIOS特徵碼        tmpmsg['version'] = bios_id.Version  # BIOS版本        tmpmsg['Manufacturer'] = bios_id.Manufacturer.strip()  # BIOS固件生產廠家        tmpmsg['ReleaseDate'] = bios_id.ReleaseDate  # BIOS釋放日期        tmpmsg['SMBIOSBIOSVersion'] = bios_id.SMBIOSBIOSVersion  # 系統管理規範版本        bioss.append(tmpmsg)    return bioss# 硬盤def get_Disk_Info():    disks = []    for disk in c.Win32_DiskDrive():        # print disk.__dict__        tmpmsg = {}        tmpmsg['SerialNumber'] = disk.SerialNumber.strip()        tmpmsg['DeviceID'] = disk.DeviceID        tmpmsg['Caption'] = disk.Caption        tmpmsg['Size'] = disk.Size        tmpmsg['UUID'] = disk.qualifiers['UUID'][1:-1]        disks.append(tmpmsg)    return disks# 內存def get_PhysicalMemory_Info():    memorys = []    for mem in c.Win32_PhysicalMemory():        tmpmsg = {}        tmpmsg['UUID'] = mem.qualifiers['UUID'][1:-1]        tmpmsg['BankLabel'] = mem.BankLabel        tmpmsg['SerialNumber'] = mem.SerialNumber.strip()        tmpmsg['ConfiguredClockSpeed'] = mem.ConfiguredClockSpeed        tmpmsg['Capacity'] = mem.Capacity        tmpmsg['ConfiguredVoltage'] = mem.ConfiguredVoltage        memorys.append(tmpmsg)    return memorys# 電池信息def get_Battery_Info():    isBatterys = False    for b in c.Win32_Battery():        isBatterys = True    return isBatterys# 網卡mac地址def get_MacAddress_Info():    macs = []    for n in c.Win32_NetworkAdapter():        mactmp = n.MACAddress        if mactmp and len(mactmp.strip()) > 5:            tmpmsg = {}            tmpmsg['MACAddress'] = n.MACAddress            tmpmsg['Name'] = n.Name            tmpmsg['DeviceID'] = n.DeviceID            tmpmsg['AdapterType'] = n.AdapterType            tmpmsg['Speed'] = n.Speed            macs.append(tmpmsg)    return macsdef main():    print(get_CPU_Info())    print(get_BIOS_Info())    print(get_Mainboard_Info())if __name__ == '__main__':    main()

運行該文件,系統出現異常,報截圖錯誤:

python中的坑(1)- 獲取CPU、主板、內存、硬盤、網卡等硬件信息

import wmi 出現異常,ModuleNotFoundError: No module named 'win32com'

沒有找到win32com模塊???可是我們明明已經安裝了pywin32,怎會回事?

★★★原因是:本工程中沒有導入pywin32模塊★★★

解決辦法:

pycharm->File->Settings...->Project Interpreter->添加pywin32

再次運行程序,成功!!


分享到:


相關文章: