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

yipeiwu_com6年前Python基础

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

以下是一个简单的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中的列表知识点汇总

Python list 在介绍 Python tuple 时,我使用了类比的方法,将其比做一个袋子,您可以在袋子中存放不同的东西。Python list 与此非常类似,因此,它的功能与袋...

详解Python 4.0 预计推出的新功能

Python 3.8 发布在即,核心开发者团队让我总结一下最近讨论的 Python 4.0 预计推出的新功能,代码名为“ Ouroboros:自噬蛇”。Python 4.0 是大家翘首以...

浅析Python基础-流程控制

Python编程语言的作用非常强大,而且其应用方便的特点也对开发人员起到了非常大的作用。在这里我们就可以先从Python流程控制关键字的相关概念开始了解,从而初步掌握这一语言的特点。 P...

Python对CSV、Excel、txt、dat文件的处理

python读取txt文件:(思路:先打开文件,读取文件,最后用for循环输出内容) 1、读取 1.1基于python csv库 #3.读取csv至字典x,y import csv...

Python3多线程基础知识点

多线程类似于同时执行多个不同程序,多线程运行有如下优点: 使用线程可以把占据长时间的程序中的任务放到后台去处理。 用户界面可以更加吸引人,比如用户点击了一个按钮去触发某些事件...