python tornado使用流生成图片的例子


Posted in Python onNovember 18, 2019

监控中,通常要使用图片更直观的看出集群的运行状况。

以下是一个简单的demo,通过rrdtool生成动态的图片。Python3, tornado.

web.py

templates/index.html

import tornado.ioloop
import tornado.web
import os
import io
import os
from PIL import Image
from tornado.options import define, options, parse_command_line
from tornado.web import RequestHandler


class PicHandles(RequestHandler):
  def get(self):
    id = self.get_argument('id')
    pic = open('F:/soft_bak/'+id+'.png', 'rb')
    pics = pic.read()
    self.write(pics)
    self.set_header("Content-type", "image/png")
class GetPicHandles(RequestHandler):
  def get(self):
    self.render("index.html")
def create_rrdtool_pic():
  # os.system("/usr/bin/rrdtool graph /tmp/a.png    --start '-3600s'     --end now     --width 400     --height 100     --title ' Grid Grid last hour last hour'     --vertical-label load_one     --slope-mode      DEF:'sum'='/var/lib/ganglia/rrds/cluster/__SummaryInfo__/load_one.rrd:sum':AVERAGE AREA:'sum'#555555:'  '     CDEF:sum_pos=sum,0,LT,0,sum,IF     VDEF:sum_last=sum_pos,LAST     VDEF:sum_min=sum_pos,MINIMUM VDEF:sum_avg=sum_pos,AVERAGE     VDEF:sum_max=sum_pos,MAXIMUM     GPRINT:'sum_last':'Now\:%7.2lf%s'     GPRINT:'sum_min':'Min\:%7.2lf%s'     GPRINT:'sum_avg':'Avg\:%7.2lf%s'     GPRINT:'sum_max':'Max\:%7.2lf%s\l' ")
  img = Image.open("F:/soft_bak/a.png")
  return img, ""
class GenPicHandler(tornado.web.RequestHandler):
  def get(self, *args, **kwargs):
    imgio=io.BytesIO()
    img,code=create_rrdtool_pic()
    img.save(imgio,'PNG')
    self.set_header('Content-Type', 'image/png')
    self.write(imgio.getvalue())

define('port', default = 9900, type = int,)
def main():
  parse_command_line()
  app = tornado.web.Application(
    [
      (r"/pic", PicHandles),
      (r"/getPic", GetPicHandles),
      (r"/getGenPic", GenPicHandler),
    ],
    debug=True,
    default_host="0.0.0.0",
    template_path=os.path.join(os.path.dirname(__file__), "templates")
  )
  app.listen(options.port)
  tornado.ioloop.IOLoop.instance().start()

if __name__ =='__main__':
  main()
<!DOCTYPE html>
<html>
  <head><title>Poem Maker Pro</title></head>
  <body>

  <a href="./pic?id=aa" rel="external nofollow" >
   <img src="./pic?id=a"
      alt="{$source.name} NETWORK" border="0" />
  </a>
  </body>
</html>

以上这篇python tornado使用流生成图片的例子就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python自动化测试工具Splinter简介和使用实例
May 13 Python
使用Python的Flask框架来搭建第一个Web应用程序
Jun 04 Python
Python3利用Dlib19.7实现摄像头人脸识别的方法
May 11 Python
解决PySide+Python子线程更新UI线程的问题
Jan 11 Python
对python中字典keys,values,items的使用详解
Feb 03 Python
python三引号输出方法
Feb 27 Python
tensorflow 利用expand_dims和squeeze扩展和压缩tensor维度方式
Feb 07 Python
在python3中实现查找数组中最接近与某值的元素操作
Feb 29 Python
ubuntu16.04升级Python3.5到Python3.7的方法步骤
Aug 20 Python
selenium3.0+python之环境搭建的方法步骤
Feb 01 Python
如何通过一篇文章了解Python中的生成器
Apr 02 Python
Python尝试实现蒙特卡罗模拟期权定价
Apr 21 Python
解决pandas展示数据输出时列名不能对齐的问题
Nov 18 #Python
python 实现绘制整齐的表格
Nov 18 #Python
wxPython色环电阻计算器
Nov 18 #Python
Python模拟登录之滑块验证码的破解(实例代码)
Nov 18 #Python
Python django搭建layui提交表单,表格,图标的实例
Nov 18 #Python
使用Django搭建一个基金模拟交易系统教程
Nov 18 #Python
wxPython实现文本框基础组件
Nov 18 #Python
You might like
将PHP作为Shell脚本语言使用
2006/10/09 PHP
一个简单的MySQL数据浏览器
2006/10/09 PHP
修改apache配置文件去除thinkphp url中的index.php
2014/01/17 PHP
windows下配置php5.5开发环境及开发扩展
2014/12/25 PHP
javascript编程起步(第四课)
2007/01/10 Javascript
javascript写的一个链表实现代码
2009/10/25 Javascript
JavaScript中的style.display属性操作
2013/03/27 Javascript
如何使用jQuery来处理图片坏链具体实现步骤
2013/05/02 Javascript
javascript设计简单的秒表计时器
2020/09/05 Javascript
js编写当天简单日历效果【实现代码】
2016/05/03 Javascript
JS实现图片上传预览功能
2016/11/21 Javascript
js实现前端图片上传即时预览功能
2017/08/02 Javascript
Node.js如何使用Diffie-Hellman密钥交换算法详解
2017/09/05 Javascript
js中对象与对象创建方法的各种方法
2019/02/27 Javascript
利用Vue的v-for和v-bind实现列表颜色切换
2020/07/17 Javascript
vue 中this.$set 动态绑定数据的案例讲解
2021/01/29 Vue.js
python中的一些类型转换函数小结
2013/02/10 Python
Python heapq使用详解及实例代码
2017/01/25 Python
Python处理Excel文件实例代码
2017/06/20 Python
python实现zabbix发送短信脚本
2018/09/17 Python
python语言线程标准库threading.local解读总结
2019/11/10 Python
ubuntu16.04升级Python3.5到Python3.7的方法步骤
2020/08/20 Python
Python 利用flask搭建一个共享服务器的步骤
2020/12/05 Python
Python爬虫+Tkinter制作一个翻译软件的示例
2021/02/20 Python
CSS3实现翘边的阴影效果的代码示例
2016/06/13 HTML / CSS
ProBikeKit德国:在线公路自行车专家
2018/06/03 全球购物
优瑞自动咖啡机官网:Jura
2018/09/29 全球购物
网络技术专业求职信
2014/02/18 职场文书
产品质量承诺范本
2014/03/31 职场文书
教师节标语大全
2014/10/07 职场文书
2015年元旦主持词开场白
2014/12/14 职场文书
孔繁森观后感
2015/06/10 职场文书
励志正能量20句:送给所有为梦想拼搏的人
2019/11/11 职场文书
话题作文之财富(600字)
2019/12/03 职场文书
MySQL配置主从服务器(一主多从)
2021/08/07 MySQL
Redis高并发缓存架构性能优化
2022/05/15 Redis