Python_way, Day25 WMI

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.