1.获取系统cpu占有率的信息

import psutil
 import time
# 隔1s绘制cpu的占有率 -->持久化的保存
 # 如何将时间和对应的cpu占有率去匹配
while True:
     # 获取当前时间和cpu占有率
     t = time.localtime()
     cur_time = '%d:%d:%d' %(t.tm_hour,t.tm_min,t.tm_sec)
     cpu_res = psutil.cpu_percent()   ##cpu占有率
     #print(cpu_res)
    # 保存到文件中
     with open('cpu.txt','a+') as f:
         f.write('%s %s \n' %(cur_time,cpu_res))
     time.sleep(1)


绘制cpu使用率图.py

import random
 from pyecharts.charts import Line
 import pyecharts.options as opts
# 获取折线图需要的绘制的数据
 x = []
 y = []  
 with open('cpu.txt') as f: #以读的方式打开文件
     for line in f: #依次遍历文件的每一行内容
         time,per = line.split() # 返回时间和对应的cpu占有率
         x.append(time)
         y.append(per)
 # 添加对应的x和y的对应点
line = (
     Line()
     .add_xaxis(x)    ##x轴
    .add_yaxis('',y) ##y轴
    .set_global_opts(title_opts=opts.TitleOpts(title='CPU占有率折线图'))  ##标题
)
 # 将折线图信息保存早文件中
 line.render()


python获取系统cpu个数 python获取cpu占用_Line

python获取系统cpu个数 python获取cpu占用_手机助手_02

 

python获取系统cpu个数 python获取cpu占用_手机助手_03

itchart模块

import itchat #第三方模块 需要网络下载
 import time
 # 1.给手机助手发送消息
itchat.auto_login()
# while True:
 #     # 给微信的手机助手发信息
 #     itchat.send('hello',toUserName='filehelper')
       # 给微信的助手发送/etc/passwd文件
 #     itchat.send_file('/etc/passwd',toUserName='filehelper')
 #     time.sleep(1)

python获取系统cpu个数 python获取cpu占用_手机助手_04

# 2.统计你的好友的男女比例

friends = itchat.get_friends()
 #print(friends)
info = {}
 for friends in friends[1:]:
     if friends['Sex'] == 1:
         info['male'] = info.get('male',0) + 1
     elif friends['Sex'] == 2:
         info['female'] = info.get('female',0) + 1
     else:
         info['other'] = info.get('other',0) + 1
 print(info)

python获取系统cpu个数 python获取cpu占用_折线图_05

python获取系统cpu个数 python获取cpu占用_python获取系统cpu个数_06


 

在python中执行shell命令
 判断命令是否执行成功
 # 返回值是0  执行成功
 # 返回值不为0 执行不成功
  
 import os
 print(os.system('ls'))
 print(os.system('hahaha'))

python获取系统cpu个数 python获取cpu占用_手机助手_07

# 保存命令执行的结果
 os.popen():会返回一个含有read方法的对象,可以使用f.read()来获得执行的结果,
            使用os.popen并不会返回状态码
 import  os
 res = os.popen('hostname').read()
 print(res)

python获取系统cpu个数 python获取cpu占用_手机助手_08

 

import os
 import itchat
@itchat.msg_register(itchat.content.TEXT,isFriendChat=True)
 def text_reply(msg):
     if msg['ToUserName'] == 'filehelper':
         # 获取要执行的命令的内容
         command = msg['Content']
         # 让电脑执行命令代码
         # 如果执行成功,返回值为0
         if os.system(command) == 0:
             res = os.popen(command).read()
             result = '[返回值]-命令执行成功,执行结果:\n' +res
             itchat.send(result,'filehelper')
        # 如果命令执行失败
         else:
             result = '[返回值]-命令%s执行失败,请重新测试' %(command)
             itchat.send(result,'filehelper')
 itchat.auto_login()
 itchat.run()