Pip Install WMI If it cannot be installed, use the install python3-m pip install WMI
Install PYWIN32 This package again
Use:
Import Platformimport Win32comimport 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):
"" "Get CPU Information" ""
data = {}
Cpu_lists = Self.wmi_obj. Win32_Processor ()
Cpu_core_count = 0 #初始化cpu核数
For CPUs 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):
"" "Get Memory Information" ""
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":d ATA}
Memory:
Linux Get server hardware information command:
DMIDECODE-T system
Python_way, Day25 WMI