Python---cpu信息的获取,itchart模块
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Python---cpu信息的获取,itchart模块,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2617字,纯文字阅读大概需要4分钟。
内容图文
![Python---cpu信息的获取,itchart模块](/upload/InfoBanner/zyjiaocheng/710/51ba585f728c4b3593ab0ddb47300cde.jpg)
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()
内容总结
以上是互联网集市为您收集整理的Python---cpu信息的获取,itchart模块全部内容,希望文章能够帮你解决Python---cpu信息的获取,itchart模块所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。