Home >Backend Development >Python Tutorial >python gets native hardware information

python gets native hardware information

高洛峰
高洛峰Original
2016-10-18 11:29:481799browse

Note: This code requires wmi and system win32 extension support.

If you don’t have the library installed, you need to download and install it first. I installed WMI-1.4.6.win32 and pywin32-218.win32-py2.7

Also, you can modify the file directory in the code yourself.

# -*- coding:gb2312 -*- 
import wmi
hardware=file('F:\Python\Hardware.txt','w')
  
w=wmi.WMI()
hardware.write("cpu型号,主频:\n")
for processor in w.Win32_Processor():         
hardware.write("Processor ID: %s" % processor.DeviceID)
hardware.write("\nProcess Name: %s" % processor.Name.strip()+'\n\n')
hardware.write('内存大小:')
totalMemSize=0
for memModule in w.Win32_PhysicalMemory():  
totalMemSize+=int(memModule.Capacity)
hardware.write("\nMemory Capacity: %.2fMB" %((totalMemSize+1048575)/1048576)+'\n\n')
hardware.write('硬盘使用情况:')
for disk in w.Win32_LogicalDisk (DriveType=3):
temp=disk.Caption+" %0.2f%% free" %(100.0 * long (disk.FreeSpace) / long (disk.Size))
hardware.write('\n'+temp)
hardware.write('\n')
hardware.write('\n显示IP和MAC:\n')
for interface in w.Win32_NetworkAdapterConfiguration (IPEnabled=1):
hardware.write('网卡驱动信息:')
hardware.write(interface.Description+'\n')
hardware.write('网卡MAC地址:')
hardware.write(interface.MACAddress+'\n')
#for ip_address in interface.IPAddress:
hardware.write('IP地址:')
hardware.write(interface.IPAddress[0]+'\n')
hardware.write('外网IP接口')
hardware.write(interface.IPAddress[1]+'\n')
hardware.close()

Operation renderings

python gets native hardware information

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn