How Python uses WMI to detect Windows System Information, hard disk information, network card information

Source: Internet
Author: User
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.

  • 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.