python tornado使用流生成圖片的例子
更新時(shí)間:2019年11月18日 16:40:14 作者:自由自在夢(mèng)在何方
今天小編就為大家分享一篇python tornado使用流生成圖片的例子,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
監(jiān)控中,通常要使用圖片更直觀的看出集群的運(yùn)行狀況。
以下是一個(gè)簡(jiǎn)單的demo,通過rrdtool生成動(dòng)態(tài)的圖片。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使用流生成圖片的例子就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python統(tǒng)計(jì)分析模塊statistics用法示例
這篇文章主要介紹了Python統(tǒng)計(jì)分析模塊statistics用法,結(jié)合實(shí)例形式分析了Python統(tǒng)計(jì)分析模塊statistics計(jì)算平均數(shù)、中位數(shù)、出現(xiàn)次數(shù)、標(biāo)準(zhǔn)差等相關(guān)操作技巧,需要的朋友可以參考下2019-09-09Django+JS 實(shí)現(xiàn)點(diǎn)擊頭像即可更改頭像的方法示例
這篇文章主要介紹了Django+JS 實(shí)現(xiàn)點(diǎn)擊頭像即可更改頭像的方法示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-12-12Python處理中文標(biāo)點(diǎn)符號(hào)大集合
中文文本中可能出現(xiàn)的標(biāo)點(diǎn)符號(hào)來源比較復(fù)雜,通過匹配等手段對(duì)他們處理的時(shí)候需要格外小心,防止遺漏,下面小編給大家?guī)砹薖ython處理中文標(biāo)點(diǎn)符號(hào)大集合,感興趣的朋友跟隨腳本之家小編一起看看吧2018-05-05