The examples in this article describe how Python uses WMI to detect Windows System Information, hard disk information, and network card information. Share to everyone for your reference. The implementation method is as follows:
#!/usr/bin/env python #-*-coding:utf-8-*-Import WMI Import sys,time,platform def get_system_info (OS): "" Gets the OS version This. "" "Print print" Operating system: "If OS = =" Windows ": C = WMI. WMI () for SYS in C.win32_operatingsystem (): print ' \ t ' + "Version: \t%s"% sys. Caption.encode ("GBK") print ' \ t ' + ' vernum: \t%s '% sys. BuildNumber def get_memory_info (OS): "" "gets the physical memory and virtual memory. "" "Print print" Memory_info: "If os = =" Windows ": C = WMI. WMI () cs = C.win32_computersystem () Pfu = C.win32_pagefileusage () memtotal = Int (cs[0]. totalphysicalmemory)/1024/1024 print ' \ t ' + "TotalPhysicalMemory:" + ' \ t ' + str (memtotal) + "M" #tmpdict ["Memfree "] = Int (os[0]. freephysicalmemory)/1024 swaptotal = Int (pfu[0]. allocatedbasesize) print ' \ t ' + "Swaptotal:" + ' \ t ' + str (swaptotal) + "M" #tmpdict ["swapfree"] = Int (pfu[0]. Allocatedbasesize-pfu[0]. Currentusage) def get_disk_info (OS): "" "gets the physical disk information. "" Print print "disk_iNFO: "If os = =" Windows ": Tmplist = [] c = WMI. WMI () for Physical_disk in C.win32_diskdrive (): If Physical_disk. Size:print ' \ t ' + str (physical_disk. Caption) + ': \ t ' + str (long (physical_disk). Size)/1024/1024/1024) + "G" def get_cpu_info (OS): "" To get CPU information. "" "Print print" Cpu_info: "If os = =" Windows ": tmpdict = {} tmpdict[" cpucores "] = 0 c = WMI. WMI () for CPUs in C.win32_processor (): tmpdict["cputype"] = CPU. Name try:tmpdict["cpucores"] = CPU. NumberOfCores except:tmpdict["Cpucores"] + = 1 tmpdict["Cpuclock"] = CPU. MaxClockSpeed print ' \ t ' + ' Cputype: \ t ' + str (tmpdict["Cputype"]) print ' \ t ' + ' cpucores: \ t ' + str (tmpdict["Cp Ucores "]) def get_network_info (OS):" "Gets the NIC information and the current number of TCP connections. "" "Print print" Network_info: "If os = =" Windows ": Tmplist = [] c = WMI. WMI () for interface in C.win32_networkadapterconfiguration (ipenabled=1): Tmpdict = {} TMPDict["Description"] = interface. Description tmpdict["IPAddress"] = interface. Ipaddress[0] tmpdict["ipsubnet"] = interface. Ipsubnet[0] tmpdict["MAC" = interface. MACAddress Tmplist.append (tmpdict) for i in Tmplist:print ' t ' + i["Description"] print ' \ t ' + ' \ T ' + ' Mac: ' + ' \ t ' + i["Mac"] print ' \ t ' + ' \ t ' + "IPAddress:" + ' \ T ' + i["IPAddress"] print ' \ t ' + ' \ t ' + ' IPSubnet: "+ ' \ T ' + i[" ipsubnet "] for interfaceperftcp in C.win32_perfrawdata_tcpip_tcpv4 (): print ' \ t ' + ' TC P Connect: \ t ' + str (interfaceperftcp.connectionsestablished) If __name__ = = "__main__": os = Platform.system () get_sy Stem_info (OS) get_memory_info (OS) get_disk_info (OS) get_cpu_info (OS) get_network_info (OS)
Hopefully this article will help you with Python programming.