天天动画片 > 八卦谈 > 如何用python自己写一个简易鲁大师 【一】获取当前电脑配置

如何用python自己写一个简易鲁大师 【一】获取当前电脑配置

八卦谈 佚名 2022-10-20 10:21:41

一 准备必要的库 

安装wmi 和 win32com 这两个必要的库

pip install wmi 

pip intsall win32com

(ps :pip 在 win+r cmd 命令里面直接输入 一般py3 以后都可以直接用 py2 可百度python pip

二 代码片段

import os, sys,win32com
import wmi  #引入需要的包
c = wmi.WMI()     #创建对象
#输出处理器信息
def printCPU():
   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
   print (tmpdict)
   return  tmpdict

#输出主板信息
def printMain_board():
   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)
   print (boards)
   return boards


#输出硬盘信息
def printDisk():
   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)
   for d in disks:
       print (d)
   return disks

#输出内存信息
def printPhysicalMemory():
   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)
   for m in memorys:
       print (m)
   return memorys

#电池信息,只有笔记本才会有电池选项
def printBattery():
   isBatterys = False
   for b in c.Win32_Battery():
       isBatterys = True
   return isBatterys

#网卡mac地址:
def printMacAddress():
   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)
   print (macs)
   return macs

def main():
#主函数调用
   printCPU()
   printMain_board()
   printDisk()
   printPhysicalMemory()
   printMacAddress()
   print (printBattery())


if __name__ == '__main__':
   main()#运行

最后运行一下 

以up这台美帝良心想为例

出来了


本文标题:如何用python自己写一个简易鲁大师 【一】获取当前电脑配置 - 八卦谈
本文地址:www.ttdhp.com/article/4632.html

天天动画片声明:登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述。
扫码关注我们