Python獲取電腦硬件信息及狀態(tài)的實(shí)現(xiàn)方法
本文以實(shí)例形式展示了Python獲取電腦硬件信息及狀態(tài)的實(shí)現(xiàn)方法,是Python程序設(shè)計(jì)中很有實(shí)用價(jià)值的技巧。分享給大家供大家參考之用。具體方法如下:
主要功能代碼如下:
#!/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()
希望本文所述對(duì)大家Python程序設(shè)計(jì)的學(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ù),其核心就是第一個(gè)參數(shù):data,傳入原始數(shù)據(jù),因此我們可以據(jù)此給出六種創(chuàng)建Dataframe的方法,需要的朋友可以參考下2023-05-05python基于tkinter制作無(wú)損音樂(lè)下載工具(附源碼)
這篇文章主要介紹了python基于tkinter制作無(wú)損音樂(lè)下載工具(附源碼),幫助大家更好的理解和學(xué)習(xí)使用python,感興趣的朋友可以了解下2021-03-03Python可視化學(xué)習(xí)之seaborn繪制矩陣圖詳解
矩陣圖即用一張圖繪制多個(gè)變量之間的關(guān)系,數(shù)據(jù)挖掘中常用于初期數(shù)據(jù)探索。本文介紹python中seaborn.pairplot和seaborn.PairGrid繪制矩陣圖,需要的可以參考一下2022-02-02Python運(yùn)算符重載的簡(jiǎn)單實(shí)例代碼
什么是運(yùn)算符重載,就是讓自定義的類(lèi)生成的對(duì)象(實(shí)例)能夠使用運(yùn)算符進(jìn)行操作,這篇文章主要給大家介紹了關(guān)于Python運(yùn)算符重載的相關(guān)資料,需要的朋友可以參考下2022-01-01使用PyV8在Python爬蟲(chóng)中執(zhí)行js代碼
PyV8是chrome用來(lái)執(zhí)行javascript的引擎,據(jù)說(shuō)是最快的js引擎,通過(guò)pyv8的封裝,可以在python中使用。下面這篇文章主要介紹了使用PyV8在Python爬蟲(chóng)中執(zhí)行js代碼的相關(guān)資料,需要的朋友可以參考下。2017-02-02詳解解Django 多對(duì)多表關(guān)系的三種創(chuàng)建方式
本文主要介紹了詳解解Django 多對(duì)多表關(guān)系的三種創(chuàng)建方式,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-08-08