Python实现的读取电脑硬件信息功能示例
本文实例讲述了python实现的读取电脑硬件信息功能。分享给大家供大家参考,具体如下:
上学那会,老师让我用java获取电脑硬件信息,cpu, 硬盘,mac等,那个时候感觉搞了好久。。。。。。
今天,用python试了一下,简单多了。分享一下:
首先安装wmi库,wmi是一种规范和基础结构,通过它可以访问、配置、管理和监视几乎所有的windows资源。大多用户习惯于使用众多的图形化管理工 具来管理windows资源,在wmi之前这些工具都是通过 win32应用程序编程接口来访问和管理windows资源的。大多数脚本 语言都不能直接调用win32 api,wmii的出现使得系统管理员可以通过一种简便的方法即利用常见的脚本语言实现常用的系统管理任务。好了,上代码吧
import wmi import time import json import win32com class pchardwork(object): global s s = wmi.wmi() def get_cpu_info(self): cpu = [] cp = s.win32_processor() for u in cp: cpu.append( { "name": u.name, "serial number": u.processorid, "corenum": u.numberofcores, "numoflogicalprocessors": u.numberoflogicalprocessors, "timestamp": time.strftime('%a, %d %b %y %h:%m:%s', time.localtime()), "cpupercent": u.loadpercentage } ) print ":::cpu info:", json.dumps(cpu, true, indent=4) return cpu def get_disk_info(self): disk = [] for pd in s.win32_diskdrive(): disk.append( { "serial": s.win32_physicalmedia()[0].serialnumber.lstrip().rstrip(), # 获取硬盘序列号,调用另外一个win32 api "id": 123456, "caption": pd.caption, "size": str(int(float(pd.size)/1024/1024/1024))+"g" } ) print":::disk info:", json.dumps(disk, true, indent=4) return disk def get_network_info(self): network = [] for nw in s.win32_networkadapterconfiguration (ipenabled=1): network.append( { "mac": nw.macaddress, "ip": nw.ipaddress } ) print":::network info:", json.dumps(network, true, indent=4) return network def get_running_process(self): process = [] for p in s.win32_process(): process.append( { p.name: p.processid } ) print":::running process:", json.dumps(process, true, indent=4) return process #运行测试: pcinfo = pchardwork() pcinfo.get_cpu_info() pcinfo.get_disk_info() pcinfo.get_network_info() pcinfo.get_running_process()
运行结果:
:::cpu info: [
{
"numoflogicalprocessors": 2,
"cpupercent": 27,
"name": "pentium(r) dual-core cpu e5300 @ 2.60ghz",
"corenum": 2,
"timestamp": "tue, 29 may 2018 15:19:52",
"serial number": "bfebfbff0001067a"
}
]
:::disk info: [
{
"caption": "wdc wd5000aakx-22erma0 ata device",
"serial": "wd-wcc2ev784095",
"id": 123456,
"size": "465g"
}
]
:::network info: [
{
"ip": [
"192.168.3.37",
"fe80::d1a:8c98:b6d9:5f28"
],
"mac": "e0:cb:4e:07:75:85"
}
]
:::running process: [
{
"system idle process": 0
},
{
"system": 4
},
{
"smss.exe": 296
},
{
"csrss.exe": 428
},
{
"wininit.exe": 484
},
{
"csrss.exe": 504
},
{
"services.exe": 548
},
{
"winlogon.exe": 580
},
{
"lsass.exe": 588
},
{
"lsm.exe": 596
},
{
"svchost.exe": 732
},
{
"svchost.exe": 812
},
{
"svchost.exe": 872
},
{
"svchost.exe": 936
},
{
"svchost.exe": 996
},
{
"audiodg.exe": 1076
},
{
"svchost.exe": 1124
},
{
"zhudongfangyu.exe": 1272
},
{
"svchost.exe": 1300
},
{
"spoolsv.exe": 1556
},
{
"svchost.exe": 1584
},
{
"360bpsvc.exe": 1656
},
{
"alibabaprotect.exe": 1680
},
{
"qqprotect.exe": 1800
},
{
"secbizsrv.exe": 1860
},
{
"tbsecsvc.exe": 1904
},
{
"httpd.exe": 2004
},
{
"mysqld.exe": 2040
},
{
"wwbizsrv.exe": 456
},
{
"taskhost.exe": 1376
},
{
"dwm.exe": 2140
},
{
"explorer.exe": 2156
},
{
"taobaoprotect.exe": 2368
},
{
"360tray.exe": 2476
},
{
"tomcat6w.exe": 2500
},
{
"httpd.exe": 2676
},
{
"360sd.exe": 2712
},
{
"aliwssv.exe": 3084
},
{
"conhost.exe": 3096
},
{
"360bdoctor.exe": 3280
},
{
"baidupinyin.exe": 3376
},
{
"svchost.exe": 3420
},
{
"360rp.exe": 4008
},
{
"qq.exe": 4136
},
{
"txplatform.exe": 4480
},
{
"360se.exe": 4624
},
{
"360se.exe": 4792
},
{
"wdswfsafe.exe": 4924
},
{
"360se.exe": 5916
},
{
"360se.exe": 4456
},
{
"360se.exe": 4604
},
{
"softmgrlite.exe": 2304
},
{
"360se.exe": 5612
},
{
"360se.exe": 2756
},
{
"hh.exe": 6752
},
{
"kmplayer.exe": 7616
},
{
"chrome.exe": 6848
},
{
"chrome.exe": 7956
},
{
"chrome.exe": 7044
},
{
"chrome.exe": 6432
},
{
"chrome.exe": 4396
},
{
"chrome.exe": 5240
},
{
"360se.exe": 968
},
{
"ttplayer.exe": 8636
},
{
"360se.exe": 7080
},
{
"chrome.exe": 8784
},
{
"360se.exe": 7148
},
{
"360se.exe": 8760
},
{
"360se.exe": 2604
},
{
"360se.exe": 4784
},
{
"360se.exe": 8804
},
{
"360se.exe": 7096
},
{
"360se.exe": 8900
},
{
"trustedinstaller.exe": 5676
},
{
"360se.exe": 5232
},
{
"flashfxp.exe": 9356
},
{
"flashfxp.exe": 4416
},
{
"360se.exe": 9868
},
{
"360se.exe": 8816
},
{
"360se.exe": 6816
},
{
"eclipsephp.exe": 11000
},
{
"javaw.exe": 9324
},
{
"360se.exe": 11112
},
{
"editplus.exe": 6748
},
{
"cmd.exe": 11740
},
{
"conhost.exe": 10532
},
{
"eclipse.exe": 10556
},
{
"python2.exe": 11772
},
{
"conhost.exe": 3676
},
{
"360se.exe": 9604
},
{
"360se.exe": 10656
},
{
"svchost.exe": 10888
},
{
"wmiprvse.exe": 11968
},
{
"wmiprvse.exe": 10988
},
{
"python2.exe": 10956
},
{
"conhost.exe": 10328
}
]
简单吧,附上wmi api说明:
补充:这里使用python2.7平台测试,可能会出现如下错误:
1. no module named wmi 错误
可使用pip命令解决:
pip install wmi
即可。
2. no module named win32com.client 错误
本站下载 。
使用如下命令安装:
pip install pywin32-223-cp27-none-win32.whl
即可。
更多关于python相关内容感兴趣的读者可查看本站专题:《python进程与线程操作技巧总结》、《python socket编程技巧总结》、《python数据结构与算法教程》、《python函数使用技巧总结》、《python字符串操作技巧汇总》、《python入门与进阶经典教程》及《python文件与目录操作技巧汇总》
希望本文所述对大家python程序设计有所帮助。