第一个python实例--监控cpu
1 #第一个python实例:监控cpu 2 #/bin/bash/env Python 3 from __future__ import print_function 4 from collections import OrderedDict 5 import pprint 6 7 def CPUinfo(): 8 ''' Return the information in /proc/CPUinfo 9 as a dictionary in the following format: 10 CPU_info['proc0']={...} 11 CPU_info['proc1']={...} 12 ''' 13 CPUinfo=OrderedDict() 14 procinfo=OrderedDict() 15 16 nprocs = 0 17 with open('/proc/cpuinfo') as f: 18 for line in f: 19 if not line.strip(): 20 # end of one processor 21 CPUinfo['proc%s' % nprocs] = procinfo 22 nprocs=nprocs+1 23 # Reset 24 procinfo=OrderedDict() 25 else: 26 if len(line.split(':')) == 2: 27 procinfo[line.split(':')[0].strip()] = line.split(':')[1].strip() 28 else: 29 procinfo[line.split(':')[0].strip()] = '' 30 31 return CPUinfo 32 33 if __name__=='__main__': 34 CPUinfo = CPUinfo() 35 for processor in CPUinfo.keys(): 36 print(CPUinfo[processor]['model name'])
笃信仁厚,慎思勤勉