python获得本机硬件信息

注意:这段代码需要wmi 和 系统 win32 扩展支持。

没安装库的要先下载安装,我装的是 wmi-1.4.6.win32 和 pywin32-218.win32-py2.7

还有,代码里面文件目录自己修改下咯。

# -*- 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()

运行效果图

Posted in 未分类

发表评论