python_way ,day25 wmi

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 = {}
  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 = []
    #提取内存所有的内容
    ram_collections = self.wmi_service_connector.ExecQuery("Select * from Win32_PhysicalMemory")
     for item in ram_collections:
    item_data = {}
    #print item
    mb = int(1024 * 1024)
     ram_size = int(item.Capacity) / mb
     item_data = {
     "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的主要内容,如果未能解决你的问题,请参考以下文章

python_way day16 JQuary

python_way,day3

python_way,day4

python_way ,day22 tonardo

python_way ,day2 字符串,列表,字典,时间模块

python_way day18 html-day4, 模板