ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

Python---cpu信息的获取,itchart模块

2019-09-26 09:03:32  阅读:313  来源: 互联网

标签:info itchart itchat Python time import os cpu


  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()

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)

# 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中执行shell命令
判断命令是否执行成功
# 返回值是0  执行成功
# 返回值不为0 执行不成功
 
import os
print(os.system('ls'))
print(os.system('hahaha'))

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

"""

 

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()

 

 

 

 

 

 

标签:info,itchart,itchat,Python,time,import,os,cpu
来源: https://blog.csdn.net/weixin_44783160/article/details/101316710

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有