转:http://blog.csdn.net/u010663768/article/details/60632133

python 2.7

  • cpu入库
    1. #!/usr/bin/python
    2. # -*- coding:UTF-8 -*-
    3. import MySQLdb
    4. import time
    5. import psutil
    6.  
    7. db_name='testdb'
    8. def create_db():
    9.  
    10. db = MySQLdb.connect("localhost","root","123456","testdb")
    11. cursor=db.cursor()
    12. cursor.execute('''DROP TABLE IF EXISTS cpu''')
    13. 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)''')
    14. db.close()
    15.  
    16. def save_to_db(data):
    17. db = MySQLdb.connect("localhost","root","123456","testdb")
    18. cursor=db.cursor()
    19. cursor.execute('INSERT INTO cpu(insert_time,cpu1,cpu2,cpu3,cpu4) VALUES (%s,%s,%s,%s,%s)',data)
    20. db.commit()
    21. db.close()
    22.  
    23. create_db()
    24.  
    25. while True:
    26. cpus = psutil.cpu_percent(interval=1,percpu=True)
    27. print(cpus[0])
    28. t = time.strftime('%M:%S',time.localtime())
    29. save_to_db((t,cpus[0],cpus[1],cpus[2],cpus[3]))
    30. print('save a data')

      flask路由

    1. #!/usr/bin/python
    2. # -*- coding:UTF-8 -*-
    3. import MySQLdb
    4. import time
    5. import psutil
    6.  
    7. db_name='testdb'
    8. def create_db():
    9.  
    10. db = MySQLdb.connect("localhost","root","jereh123","testdb")
    11. cursor=db.cursor()
    12. cursor.execute('''DROP TABLE IF EXISTS cpu''')
    13. 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)''')
    14. db.close()
    15.  
    16. def save_to_db(data):
    17. db = MySQLdb.connect("localhost","root","jereh123","testdb")
    18. cursor=db.cursor()
    19. cursor.execute('INSERT INTO cpu(insert_time,cpu1,cpu2,cpu3,cpu4) VALUES (%s,%s,%s,%s,%s)',data)
    20. db.commit()
    21. db.close()
    22.  
    23. create_db()
    24.  
    25. while True:
    26. cpus = psutil.cpu_percent(interval=1,percpu=True)
    27. print(cpus[0])
    28. t = time.strftime('%M:%S',time.localtime())
    29. save_to_db((t,cpus[0],cpus[1],cpus[2],cpus[3]))
    30. print('save a data')

    前端页面

    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="utf-8">
    5. <title>ECharts3 Ajax</title>
    6. <script src="{{ url_for('static', filename='jquery-3.1.1.js') }}"></script>
    7. <script src="{{ url_for('static', filename='echarts.js') }}"></script>
    8. </head>
    9. <body>
    10. <div id="main" style="height:500px;border:1px solid #ccc;padding:10px"></div>
    11. <script type="text/javascript">
    12. var myChart = echarts.init(document.getElementById('main'));
    13. myChart.setOption({
    14. title: {
    15. text: 'CPU系统监控'
    16. },
    17. tooltip: {},
    18. legend: {
    19. data:['cpu1','cpu2','cpu3','cpu4']
    20. },
    21. xAxis: {
    22. data: []
    23. },
    24. yAxis: {},
    25. series: [{
    26. name: 'cpu1',
    27. type: 'line',
    28. data: []
    29. },{
    30. name: 'cpu2',
    31. type: 'line',
    32. data: []
    33. },{
    34. name: 'cpu3',
    35. type: 'line',
    36. data: []
    37. },{
    38. name: 'cpu4',
    39. type: 'line',
    40. data: []
    41. }]
    42. })
    43.  
    44. var insert_time = ["","","","","","","","","",""],
    45. cpu1 = [0,0,0,0,0,0,0,0,0,0],
    46. cpu2 = [0,0,0,0,0,0,0,0,0,0],
    47. cpu3 = [0,0,0,0,0,0,0,0,0,0],
    48. cpu4 = [0,0,0,0,0,0,0,0,0,0],
    49.  
    50. lastID = 0;
    51.  
    52. var update_mychart = function (data) {
    53. myChart.hideLoading();
    54. dataLength = data.insert_time.length;
    55. lastID += dataLength;
    56. insert_time = insert_time.slice(dataLength).concat(data.insert_time);
    57. cpu1 = cpu1.slice(dataLength).concat(data.cpu1.map(parseFloat));
    58. cpu2 = cpu2.slice(dataLength).concat(data.cpu2.map(parseFloat));
    59. cpu3 = cpu3.slice(dataLength).concat(data.cpu3.map(parseFloat));
    60. cpu4 = cpu4.slice(dataLength).concat(data.cpu4.map(parseFloat));
    61.  
    62. myChart.setOption({
    63. xAxis: {
    64. data: insert_time
    65. },
    66. series: [{
    67. name: 'cpu1', // 根据名字对应到相应的系列
    68. data: cpu1
    69. },{
    70. name: 'cpu2',
    71. data: cpu2
    72. },{
    73. name: 'cpu3',
    74. data: cpu3
    75. },{
    76. name: 'cpu4',
    77. data: cpu4
    78. }]
    79. });
    80.  
    81. if (dataLength == 0){clearInterval(timeTicket);}
    82. }
    83. myChart.showLoading();
    84. $.get('/cpu').done(update_mychart);
    85. var timeTicket = setInterval(function () {
    86. $.post('/cpu',{id: lastID}).done(update_mychart);
    87. }, 3000);
    88.  
    89. </script>
    90. </body>
    91. </html>

      文档目录

    1. echart/
    2. ├── mydb.py
    3. ├── static
    4.    ├── echarts.js
    5.    ├── jquery-3.1.1.js
    6.    ├── jquery-3.1.1.min.js
    7.    └── templates
    8. ├── templates
    9.    └── index.html
    10. └── 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的更多相关文章

  1. 个人学期总结及Python+Flask+MysqL的web建设技术过程

    一个学期即将过去,我们也迎来了2018年.这个学期,首次接触了web网站开发建设,不仅是这门课程,还有另外一门用idea的gradle框架来制作网页. 很显然,用python语言的flask框架更加简 ...

  2. Python+Flask+MysqL的web建设技术过程

    一.前言(个人学期总结) 个人总结一下这学期对于Python+Flask+MysqL的web建设技术过程的学习体会,Flask小辣椒框架相对于其他框架而言,更加稳定,不会有莫名其妙的错误,容错性强,运 ...

  3. 使用Java编写一个简单的Web的监控系统cpu利用率,cpu温度,总内存大小

    原文:http://www.jb51.net/article/75002.htm 这篇文章主要介绍了使用Java编写一个简单的Web的监控系统的例子,并且将重要信息转为XML通过网页前端显示,非常之实 ...

  4. CodeIgniter框架——创建一个简单的Web站点(include MySQL基本操作)

    目标 使用 CodeIgniter 创建一个简单的 Web 站点.该站点将有一个主页,显示一些宣传文本和一个表单,该表单将发布到数据库表中. 按照 CodeIgniter 的术语,可将这些需求转换为以 ...

  5. Python+Flask+MysqL的web技术建站过程

    1.个人学期总结 时间过得飞快,转眼间2017年就要过去.这一年,我学习JSP和Python,哪一门都像一样新的东西,之前从来没有学习过. 这里我就用我学习过的Python和大家分享一下,我是怎么从一 ...

  6. Linux简单Shell脚本监控MySQL、Apache Web和磁盘空间

    Linux简单Shell脚本监控MySQL.Apache Web和磁盘空间 1. 目的或任务 当MySQL数据库.Apache Web服务器停止运行时,重新启动运行,并发送邮件通知: 当服务器磁盘的空 ...

  7. 如何用PHP/MySQL为 iOS App 写一个简单的web服务器(译) PART1

    原文:http://www.raywenderlich.com/2941/how-to-write-a-simple-phpmysql-web-service-for-an-ios-app 作为一个i ...

  8. 前端和后端的数据交互(jquery ajax+python flask+mysql)

    上web课的时候老师布置的一个实验,要求省市连动,基本要求如下: 1.用select选中一个省份. 2.省份数据传送到服务器,服务器从数据库中搜索对应城市信息. 3.将城市信息返回客户,客户用sele ...

  9. 实战接口开发:python + flask + mysql + redis(根据反馈,持续细化更新。。。)

    前言 自动化已经成为测试的必备技能之一了,所以,很多想跳槽的测试朋友都在自学,特别是最实用的接口自动化, 但是很多人因为没有可以练手的项目而苦恼,最终导致缺乏实战经验,其实,完全可以自己开发个简单项目 ...

随机推荐

  1. 查看后台PHP进程(非PHP-FPM)

    ps -ef | grep php | grep -v php-fpm

  2. jedis提纲

    A01 - jedis库介绍 A01 - 在多线程下使用Jedis A01 - Jedis的八种调用方式   A02 - API使用文档 A02 - Jedis代码编程使用(简单的使用)   A03 ...

  3. Python入门及安装

    简介 是用来编写应用程序的高级编程语言,"内置电池",哲学:简单优雅,尽量写容易看明白的代码,尽量写少的代码,适合干嘛:网络应用.网站.后台服务:日常些工具,如系统管理员需要的脚本 ...

  4. poj 2395 bfs/记录路径

    http://poj.org/problem?id=2935 Basic Wall Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submi ...

  5. vue-router使用next()跳转到指定路径时会无限循环

    我在路由为 /path 的页面这样写 beforeRouteLeave (to, from, next) { console.log('离开路路由') if(to.fullPath==='/home' ...

  6. hive_学习_02_hive整合hbase(失败)

    一.前言 本文承接上一篇:hive_学习_01_hive环境搭建(单机) ,主要是记录 hive 整合hbase的流程 二.环境准备 1.环境准备 操作系统 : linux CentOS 6.8 jd ...

  7. 《深入理解 C# 第2版》 - 书摘精要

    (P13) 在很大程度上,C# 2 更像是对 C# 1 的各种不足之处的修修补补,所以并没有一鸣惊人.而 C# 3 中几乎所有特性都是为了构建 LINQ,并且其结果也十分特别: (P24) 为了让委托 ...

  8. 剑指offer--21.链表中倒数第k个结点

    定义两个指针,当一个指针指到第K个结点时,第二个指针开始向后移动 -------------- 时间限制:1秒 空间限制:32768K 热度指数:602826 本题知识点: 链表 题目描述 输入一个链 ...

  9. 剑指offer--16.数组中重复的数字

    时间限制:1秒 空间限制:32768K 热度指数:198342 本题知识点: 数组 题目描述 在一个长度为n的数组里的所有数字都在0到n-1的范围内. 数组中某些数字是重复的,但不知道有几个数字是重复 ...

  10. codevs 2503 失恋28天-缝补礼物

    题目描述 Description 话说上回他给女孩送了n件礼物,由于是廉价的所以全部都坏掉了,女孩很在意这些礼物,所以决定自己缝补,但是人生苦短啊,女孩时间有限,她总共有m分钟能去缝补礼物.由于损坏程 ...