This article describes how Python uses WMI to detect Windows System Information, hard disk information, and network card information. Share to everyone for your reference. The implementation methods are as follows:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21st
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92#!/usr/bin/env python
#-*-Coding:utf-8-*-
Import WMI
Import Sys,time,platform
def get_system_info (OS):
"""
Gets the operating system version.
"""
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):
"""
Obtain physical 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) + "G"/1024/1024/1024)
def get_cpu_info (OS):
"""
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["Cpucores"])
def get_network_info (OS):
"""
Gets the network card 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 ' + ' TCP connect:t ' + str (interfaceperftcp.connectionsestablished)
if __name__ = = "__main__":
OS = Platform.system ()
Get_system_info (OS)
Get_memory_info (OS)
Get_disk_info (OS)
Get_cpu_info (OS)
Get_network_info (OS)
I hope this article will help you with your Python programming.