Python獲取電腦硬件信息及狀態(tài)的實(shí)現(xiàn)方法
本文以實(shí)例形式展示了Python獲取電腦硬件信息及狀態(tài)的實(shí)現(xiàn)方法,是Python程序設(shè)計中很有實(shí)用價值的技巧。分享給大家供大家參考之用。具體方法如下:
主要功能代碼如下:
#!/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程序設(shè)計的學(xué)習(xí)有所幫助。
相關(guān)文章
python向企業(yè)微信發(fā)送文字和圖片消息的示例
這篇文章主要介紹了python向企業(yè)微信發(fā)送文字和圖片消息的示例,幫助大家更好的理解和使用python,感興趣的朋友可以了解下2020-09-09python基于concurrent模塊實(shí)現(xiàn)多線程
這篇文章主要介紹了python基于concurrent模塊實(shí)現(xiàn)多線程,幫助大家更好的理解和學(xué)習(xí)使用python,感興趣的朋友可以了解下2021-04-04Python Pandas創(chuàng)建Dataframe數(shù)據(jù)框的六種方法匯總
這篇文章主要介紹了Python中的Pandas創(chuàng)建Dataframe數(shù)據(jù)框的六種方法,創(chuàng)建Dataframe主要是使用pandas中的DataFrame函數(shù),其核心就是第一個參數(shù):data,傳入原始數(shù)據(jù),因此我們可以據(jù)此給出六種創(chuàng)建Dataframe的方法,需要的朋友可以參考下2023-05-05python基于tkinter制作無損音樂下載工具(附源碼)
這篇文章主要介紹了python基于tkinter制作無損音樂下載工具(附源碼),幫助大家更好的理解和學(xué)習(xí)使用python,感興趣的朋友可以了解下2021-03-03Python可視化學(xué)習(xí)之seaborn繪制矩陣圖詳解
矩陣圖即用一張圖繪制多個變量之間的關(guān)系,數(shù)據(jù)挖掘中常用于初期數(shù)據(jù)探索。本文介紹python中seaborn.pairplot和seaborn.PairGrid繪制矩陣圖,需要的可以參考一下2022-02-02詳解解Django 多對多表關(guān)系的三種創(chuàng)建方式
本文主要介紹了詳解解Django 多對多表關(guān)系的三種創(chuàng)建方式,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-08-08