首页 > 代码库 > python_way ,day25 wmi

python_way ,day25 wmi

pip install wmi  如果不能安装,就使用 安装 python3 -m pip install wmi

再安装pywin32这个包

使用:

import platform
import win32com
import wmi
class Win32Info(object):
def __init__(self):
#wmi固定用法这样就可以获取服务器的信息了
self.wmi_obj = wmi.WMI()
self.wmi_service_obj = win32com.client.Dispatch("WbemScripting.SWbemLocator")
self.wmi_service_connector =self.wmi_service_obj.ConnectServer(".", "root\cimv2")
  
  def get_cpu_info(self):
    """获取cpu信息"""
  data = http://www.mamicode.com/{}
  cpu_lists = self.wmi_obj.Win32_Processor()
  cpu_core_count = 0 #初始化cpu核数
    for cpu in cpu_lists:  #有几颗cpu就循环几回
    cpu_core_count += cpu.NumberOfCores #内循环一次增加一个cpu核数
    cpu_model = cpu.Name
    data["cpu_count"] = len(cpu_lists)
    data["cpu_model"] = cpu_model
    data["cpu_core_count"] =cpu_core_count
    return data
  
    def get_ram_info(self):
      """获取内存信息"""
    data = http://www.mamicode.com/[]
    #提取内存所有的内容
    ram_collections = self.wmi_service_connector.ExecQuery("Select * from Win32_PhysicalMemory")
     for item in ram_collections:
    item_data = http://www.mamicode.com/{}
    #print item
    mb = int(1024 * 1024)
     ram_size = int(item.Capacity) / mb
     item_data = http://www.mamicode.com/{
     "slot":item.DeviceLocator.strip(),
     "capacity":ram_size,
     "model":item.Caption,
     "manufactory":item.Manufacturer,
     "sn":item.SerialNumber,
     }
    data.append(item_data)
     #for i in data:
     # print i
    return {"ram":data}


  






 

 

 

 

  内存:

技术分享

 

linux 获取服务器硬件信息命令:

dmidecode -t system 

 

 

python_way ,day25 wmi