Python获取电脑硬件信息及状态的实现方法
作者:shichen2014 时间:2023-01-23 21:24:15
本文以实例形式展示了Python获取电脑硬件信息及状态的实现方法,是Python程序设计中很有实用价值的技巧。分享给大家供大家参考之用。具体方法如下:
主要功能代码如下:
#!/usr/bin/env python
# encoding: utf-8
from optparse import OptionParser
import os
import re
import json
def main():
try:
parser = OptionParser(usage="%prog [options]")
reg_result=re.compile('\[(.*)\]')
#add option
parser.add_option("-m","--machine",action="store",type="string",dest="machine",help="the machine to be check")
parser.add_option("-f","--file",action="store",type="string",dest="file",help="the file with machine list")
parser.add_option("-n","--noah_path",action="store",type="string",dest="noah",help="the bns path or group")
(options,args)=parser.parse_args()
result=""
if options.machine:
options.machine=options.machine.replace(".baidu.com","")
result=os.popen("meta-query entity host "+options.machine+" -f sysSuit,memTotal,diskTotal,cpuFrequency,cpuPhysicalCores,netIdc,status -j").read()
elif options.file:
result=os.popen("meta-query entity host -f sysSuit,memTotal,diskTotal,cpuFrequency,cpuPhysicalCores,netIdc,status -F "+options.file+" -j").read()
elif options.noah:
result=os.popen("get_instance_by_service "+options.noah+" |meta-query entity host -f sysSuit,memTotal,diskTotal,cpuFrequency,cpuPhysicalCores,netIdc,status -F -j").read()
else:
return
result=json.loads(result)
print "%-*s%-*s%-*s%-*s%-*s%-*s"%(40,"Name",10,"CPU",10,"memery",10,"disk",10,"IDC",10,"status")
for item in result:
if item['Values']['cpuFrequency']!="null":
item['Values']['cpuFrequency']=str(float(item['Values']['cpuFrequency'])/1000.0)[0:3]
else:
item['Values']['cpuFrequency']="0"
item['Values']['diskTotal']=str(float(item['Values']['diskTotal'])/1000000000.0)[0:5]
item['Values']['memTotal']=str(float(item['Values']['memTotal'])/1024/1000.0)[0:5]
print "%-*s%-*s%-*s%-*s%-*s%-*s" % (40,item['Name'],10,item['Values']['cpuFrequency']+" x"+item['Values']['cpuPhysicalCores'],10,item['Values']['memTotal']+"G",10,item['Values']['diskTotal']+"T",10,item['Values']['netIdc'],10,item['Values']['status'])
except Exception,e:
return
if __name__ =="__main__":
main()
希望本文所述对大家Python程序设计的学习有所帮助。
标签:Python,获取,硬件,信息,状态
0
投稿
猜你喜欢
Mysql的Table doesn't exist问题及解决
2024-01-16 05:03:13
MySQL数据库完全卸载的方法
2024-01-28 05:59:21
Django Celery异步任务队列的实现
2023-09-28 19:24:53
python使用代理ip访问网站的实例
2022-02-08 08:16:36
python求众数问题实例
2022-02-06 22:25:40
python利用wx实现界面按钮和按钮监听和字体改变的方法
2023-10-26 17:34:24
pycharm查看变量值的4种方法汇总
2022-04-17 04:37:37
浅谈python中set使用
2023-05-31 10:57:41
ASP使用fso遍历文件及文件夹列出文件名
2008-10-27 19:32:00
JavaScript中尽量用局部变量的原因[译]
2009-02-20 13:45:00
Python实战小项目之Mnist手写数字识别
2023-01-20 23:24:56
Python实战之多种音乐格式批量转换
2023-07-12 20:24:42
pandas如何统计某一列或某一行的缺失值数目
2023-02-10 21:36:00
Vue组件化开发思考
2024-04-27 15:47:38
Python3.x+pyqtgraph实现数据可视化教程
2023-09-25 23:24:47
python利用线程实现多任务
2023-10-02 07:15:40
MySQL一键安装Shell脚本的实现
2024-01-16 23:28:29
SQLSERVER 创建索引实现代码
2012-04-13 12:17:05
Android分包MultiDex策略详解
2021-11-14 13:28:42
如何应对SQL Server数据库崩溃
2008-11-24 17:25:00