python tornado使用流生成圖片的例子
監(jiān)控中,通常要使用圖片更直觀的看出集群的運(yùn)行狀況。
以下是一個(gè)簡單的demo,通過rrdtool生成動態(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使用流生成圖片的例子就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
python使用BeautifulSoup分頁網(wǎng)頁中超鏈接的方法
這篇文章主要介紹了python使用BeautifulSoup分頁網(wǎng)頁中超鏈接的方法,涉及Python使用BeautifulSoup模塊操作網(wǎng)頁鏈接的技巧,需要的朋友可以參考下2015-04-04
Python Django安裝配置模板系統(tǒng)及使用實(shí)戰(zhàn)全面詳解
本文首先介紹了Django模板系統(tǒng)的基礎(chǔ)知識,接著探討了如何安裝和配置Django模板系統(tǒng),然后深入解析了Django模板的基本結(jié)構(gòu)、標(biāo)簽和過濾器的用法,闡述了如何在模板中展示模型數(shù)據(jù),最后使用一個(gè)實(shí)際項(xiàng)目的例子來演示如何在實(shí)際開發(fā)中使用Django模板系統(tǒng)2023-09-09
python matplotlib繪圖實(shí)現(xiàn)刪除重復(fù)冗余圖例的操作
這篇文章主要介紹了python matplotlib繪圖實(shí)現(xiàn)刪除重復(fù)冗余圖例的操作,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-04-04
基于Python實(shí)現(xiàn)多圖繪制系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了如何基于Python實(shí)現(xiàn)一個(gè)簡單的多圖繪制系統(tǒng),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-02-02
windows下添加Python環(huán)境變量的方法匯總
默認(rèn)情況下,在windows下安裝python之后,系統(tǒng)并不會自動添加相應(yīng)的環(huán)境變量。此時(shí)不能在命令行直接使用python命令。今天我們就來看下,如何簡單快捷的在windows下添加Python環(huán)境變量2018-05-05
詳解Django將秒轉(zhuǎn)換為xx天xx時(shí)xx分
這篇文章主要介紹了Django將秒轉(zhuǎn)換為xx天xx時(shí)xx分,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-09-09
用Python進(jìn)行柵格數(shù)據(jù)的分區(qū)統(tǒng)計(jì)和批量提取
該教程其實(shí)源于web,我看到之后覺得很實(shí)用,于是自己又重復(fù)做了一遍,寫了詳細(xì)的注釋分享給大家,希望對大家的研究有幫助,本文講述了柵格的分區(qū)統(tǒng)計(jì),批量提取,深化理解遍歷循環(huán)等內(nèi)容2021-05-05
python中將\\uxxxx轉(zhuǎn)換為Unicode字符串的方法
這篇文章主要介紹了python中將\\uxxxx轉(zhuǎn)換為Unicode字符串的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-09-09

