pythontornado使用流生成图片的例子-创新互联
监控中,通常要使用图片更直观的看出集群的运行状况。

以下是一个简单的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()
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
当前题目:pythontornado使用流生成图片的例子-创新互联
路径分享:http://www.jxjierui.cn/article/gidjg.html


咨询
建站咨询
