简单cpu web flask mysql
转:http://blog.csdn.net/u010663768/article/details/60632133
python 2.7
- cpu入库
#!/usr/bin/python
# -*- coding:UTF-8 -*-
import MySQLdb
import time
import psutil db_name='testdb'
def create_db(): db = MySQLdb.connect("localhost","root","123456","testdb")
cursor=db.cursor()
cursor.execute('''DROP TABLE IF EXISTS cpu''')
cursor.execute('''CREATE TABLE cpu(id INT NOT NULL AUTO_INCREMENT,PRIMARY KEY (`id`),insert_time text,cpu1 float,cpu2 float,cpu3 float,cpu4 float)''')
db.close() def save_to_db(data):
db = MySQLdb.connect("localhost","root","123456","testdb")
cursor=db.cursor()
cursor.execute('INSERT INTO cpu(insert_time,cpu1,cpu2,cpu3,cpu4) VALUES (%s,%s,%s,%s,%s)',data)
db.commit()
db.close() create_db() while True:
cpus = psutil.cpu_percent(interval=1,percpu=True)
print(cpus[0])
t = time.strftime('%M:%S',time.localtime())
save_to_db((t,cpus[0],cpus[1],cpus[2],cpus[3]))
print('save a data')flask路由
#!/usr/bin/python
# -*- coding:UTF-8 -*-
import MySQLdb
import time
import psutil db_name='testdb'
def create_db(): db = MySQLdb.connect("localhost","root","jereh123","testdb")
cursor=db.cursor()
cursor.execute('''DROP TABLE IF EXISTS cpu''')
cursor.execute('''CREATE TABLE cpu(id INT NOT NULL AUTO_INCREMENT,PRIMARY KEY (`id`),insert_time text,cpu1 float,cpu2 float,cpu3 float,cpu4 float)''')
db.close() def save_to_db(data):
db = MySQLdb.connect("localhost","root","jereh123","testdb")
cursor=db.cursor()
cursor.execute('INSERT INTO cpu(insert_time,cpu1,cpu2,cpu3,cpu4) VALUES (%s,%s,%s,%s,%s)',data)
db.commit()
db.close() create_db() while True:
cpus = psutil.cpu_percent(interval=1,percpu=True)
print(cpus[0])
t = time.strftime('%M:%S',time.localtime())
save_to_db((t,cpus[0],cpus[1],cpus[2],cpus[3]))
print('save a data')前端页面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>ECharts3 Ajax</title>
<script src="{{ url_for('static', filename='jquery-3.1.1.js') }}"></script>
<script src="{{ url_for('static', filename='echarts.js') }}"></script>
</head>
<body>
<div id="main" style="height:500px;border:1px solid #ccc;padding:10px"></div>
<script type="text/javascript">
var myChart = echarts.init(document.getElementById('main'));
myChart.setOption({
title: {
text: 'CPU系统监控'
},
tooltip: {},
legend: {
data:['cpu1','cpu2','cpu3','cpu4']
},
xAxis: {
data: []
},
yAxis: {},
series: [{
name: 'cpu1',
type: 'line',
data: []
},{
name: 'cpu2',
type: 'line',
data: []
},{
name: 'cpu3',
type: 'line',
data: []
},{
name: 'cpu4',
type: 'line',
data: []
}]
}) var insert_time = ["","","","","","","","","",""],
cpu1 = [0,0,0,0,0,0,0,0,0,0],
cpu2 = [0,0,0,0,0,0,0,0,0,0],
cpu3 = [0,0,0,0,0,0,0,0,0,0],
cpu4 = [0,0,0,0,0,0,0,0,0,0], lastID = 0; var update_mychart = function (data) {
myChart.hideLoading();
dataLength = data.insert_time.length;
lastID += dataLength;
insert_time = insert_time.slice(dataLength).concat(data.insert_time);
cpu1 = cpu1.slice(dataLength).concat(data.cpu1.map(parseFloat));
cpu2 = cpu2.slice(dataLength).concat(data.cpu2.map(parseFloat));
cpu3 = cpu3.slice(dataLength).concat(data.cpu3.map(parseFloat));
cpu4 = cpu4.slice(dataLength).concat(data.cpu4.map(parseFloat)); myChart.setOption({
xAxis: {
data: insert_time
},
series: [{
name: 'cpu1', // 根据名字对应到相应的系列
data: cpu1
},{
name: 'cpu2',
data: cpu2
},{
name: 'cpu3',
data: cpu3
},{
name: 'cpu4',
data: cpu4
}]
}); if (dataLength == 0){clearInterval(timeTicket);}
}
myChart.showLoading();
$.get('/cpu').done(update_mychart);
var timeTicket = setInterval(function () {
$.post('/cpu',{id: lastID}).done(update_mychart);
}, 3000); </script>
</body>
</html>文档目录
echart/
├── mydb.py
├── static
│ ├── echarts.js
│ ├── jquery-3.1.1.js
│ ├── jquery-3.1.1.min.js
│ └── templates
├── templates
│ └── index.html
└── web.py- 文件
ECharts下载地址: http://echarts.baidu.com/
jQuery 3.1.1 官方下载地址:
https://code.jquery.com/jquery-3.1.1.js
https://code.jquery.com/jquery-3.1.1.min.js
简单cpu web flask mysql的更多相关文章
- 个人学期总结及Python+Flask+MysqL的web建设技术过程
一个学期即将过去,我们也迎来了2018年.这个学期,首次接触了web网站开发建设,不仅是这门课程,还有另外一门用idea的gradle框架来制作网页. 很显然,用python语言的flask框架更加简 ...
- Python+Flask+MysqL的web建设技术过程
一.前言(个人学期总结) 个人总结一下这学期对于Python+Flask+MysqL的web建设技术过程的学习体会,Flask小辣椒框架相对于其他框架而言,更加稳定,不会有莫名其妙的错误,容错性强,运 ...
- 使用Java编写一个简单的Web的监控系统cpu利用率,cpu温度,总内存大小
原文:http://www.jb51.net/article/75002.htm 这篇文章主要介绍了使用Java编写一个简单的Web的监控系统的例子,并且将重要信息转为XML通过网页前端显示,非常之实 ...
- CodeIgniter框架——创建一个简单的Web站点(include MySQL基本操作)
目标 使用 CodeIgniter 创建一个简单的 Web 站点.该站点将有一个主页,显示一些宣传文本和一个表单,该表单将发布到数据库表中. 按照 CodeIgniter 的术语,可将这些需求转换为以 ...
- Python+Flask+MysqL的web技术建站过程
1.个人学期总结 时间过得飞快,转眼间2017年就要过去.这一年,我学习JSP和Python,哪一门都像一样新的东西,之前从来没有学习过. 这里我就用我学习过的Python和大家分享一下,我是怎么从一 ...
- Linux简单Shell脚本监控MySQL、Apache Web和磁盘空间
Linux简单Shell脚本监控MySQL.Apache Web和磁盘空间 1. 目的或任务 当MySQL数据库.Apache Web服务器停止运行时,重新启动运行,并发送邮件通知: 当服务器磁盘的空 ...
- 如何用PHP/MySQL为 iOS App 写一个简单的web服务器(译) PART1
原文:http://www.raywenderlich.com/2941/how-to-write-a-simple-phpmysql-web-service-for-an-ios-app 作为一个i ...
- 前端和后端的数据交互(jquery ajax+python flask+mysql)
上web课的时候老师布置的一个实验,要求省市连动,基本要求如下: 1.用select选中一个省份. 2.省份数据传送到服务器,服务器从数据库中搜索对应城市信息. 3.将城市信息返回客户,客户用sele ...
- 实战接口开发:python + flask + mysql + redis(根据反馈,持续细化更新。。。)
前言 自动化已经成为测试的必备技能之一了,所以,很多想跳槽的测试朋友都在自学,特别是最实用的接口自动化, 但是很多人因为没有可以练手的项目而苦恼,最终导致缺乏实战经验,其实,完全可以自己开发个简单项目 ...
随机推荐
- Nova中的系统状态分析
系统状态 作者 孔令贤 Nova提供这么几个资源状态的查询. Service Nova中的service有两类,一类是所谓的control service,一类就是compute service.要想 ...
- js写出斐波那契数列
斐波那契数列:1.1.2.3.5.8.13.21.34.…… 函数: 使用公式f[n]=f[n-1]+f[n-2],依次递归计算,递归结束条件是f[1]=1,f[2]=1. for循环: 从底层向上运 ...
- 178. Rank Scores
问题描述 解决方案 select sc.Score, (select count(*) from (select distinct Score from Scores ) ds where ds.Sc ...
- deep learning 学习笔记(三) 线性回归学习速率优化寻找
继续学习http://www.cnblogs.com/tornadomeet/archive/2013/03/15/2962116.html,上一节课学习速率是固定的,而这里我们的目的是找到一个比较好 ...
- C# 之二进制序列化
序列化:又称串行化,是.NET运行时环境用来支持用户定义类型的流化的机制.其目的是以某种存储形成使自定义对象持久化,或者将这种对象从一个地方传输到另一个地方. 一般有三种方式:1.是使用BinaryF ...
- Leetcode 509. Fibonacci Number
class Solution(object): def fib(self, N): """ :type N: int :rtype: int ""&q ...
- Linux命令学习(21):netstat命令
版权声明 更新:2017-06-13博主:LuckyAlan联系:liuwenvip163@163.com声明:吃水不忘挖井人,转载请注明出处! 1 文章介绍 本文介绍了Linux下面的netstat ...
- bzoj 2655 calc——拉格朗日插值
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2655 先考虑DP.dp[ i ][ j ]表示值域为 i .选 j 个值的答案,则 dp[ ...
- BZOJ1901:Dynamic Rankings
浅谈离线分治算法:https://www.cnblogs.com/AKMer/p/10415556.html 题目传送门:https://lydsy.com/JudgeOnline/problem.p ...
- 关于DO、VO的一些新的认识
今天在开发前的定案以及业务介绍过程中,讲到了一些关于VO和DO的知识和理解. 听到之后就觉得很惊奇,也或许是自己以前不够深入理解开发的过程.认知的很浅薄. DO VO以前的认知里面,都是一样的,都属于 ...