【2019-05-29】tornado简单使用笔记 [历史归档]本文原发布于 cstriker1407.info 个人博客内容为历史存档仅供参考。发布时间2019-05-29 标题tornado简单使用笔记分类编程 / web / tornado 标签python jython·bootstrap·tornadotornado简单使用笔记tornado是什么安装方法tornado简单DEMOhtml页面输出方法使用python代码返回静态html页面使用python代码返回定制html页面使用tornado的render功能返回html页面和bootstrap一起使用最近要用python来做一个WEB服务器找来找去感觉tornado上手比较简单这里简单笔记下使用要点。tornado是什么Tornado是使用Python编写的一个强大的、可扩展的Web服务器。入门文档【http://shouce.jb51.net/tornado/index.html 】官方文档【 https://tornado-zh.readthedocs.io/zh/latest/index.html 】安装方法tornado是一个python组件因此最简单方式就是pip安装。#pip3 install tornadotornado简单DEMO根据入门文档我们可以非常快速的实现一个WEB服务器新建hello_tornado.py代码如下#!/usr/bin/python3# -*- coding: UTF-8 -*-importsysimportosimporttornado.httpserverimporttornado.ioloopimporttornado.optionsimporttornado.webfromtornado.templateimportTemplatedefhandler_list():return[(r/hello_tornado,hello_tornado_handler_main),]classhello_tornado_handler_main(tornado.web.RequestHandler):defget(self):self.write(Rev Get Request:{}.format(self.request))if__name____main__:apptornado.web.Application(handlershandler_list(),debugTrue)http_servertornado.httpserver.HTTPServer(app)http_server.listen(8000)tornado.ioloop.IOLoop.instance().start()然后执行命令#python3 hello_tornado.py我们访问http://127.0.0.1:8000/hello_tornado可以看到结果html页面输出方法tornado有好几种方式可以输出html页面这里简单笔记下。使用python代码返回静态html页面这种方式很简单直接把html内容返回即可。classhello_tornado_handler_main(tornado.web.RequestHandler):defget(self):self.write( !DOCTYPE html html bodyHello world/body /html )显示如下使用python代码返回定制html页面这种方式是利用tornado的template功能来替换html中的字符串。class hello_tornado_handler_main(tornado.web.RequestHandler): def get(self): contentTemplate(!DOCTYPE htmlhtmlbodyhello,{{name1}}and{{name2}}/body/html)self.write(content.generate(name1LiLei,name2HanMM))显示如下这两种方式都不是太友好因为将html写入到代码中会耦合显示与业务所以我们一般使用下面的这种方式。使用tornado的render功能返回html页面新建一个hello_tornado.html文件内容如下!DOCTYPEhtmlhtmlbodyhello,{{name1}} and {{name2}}/body/htmlpython代码为classhello_tornado_handler_main(tornado.web.RequestHandler):defget(self):self.render(htmls/hello_tornado.html,name1LiLei,name2HanMM)文件结构├── hello_tornado.py ├── htmls │ ├── hello_tornado.html显示如下和bootstrap一起使用如果要和bootstrap同时使用可以设置 static_path 变量。hello_tornado.html内容!DOCTYPEhtmlhtmllangzh-CNheadmetacharsetutf-8metahttp-equivX-UA-CompatiblecontentIEedgemetanameviewportcontentwidthdevice-width, initial-scale1!-- Bootstrap --link href{{ static_url(bootstrap-3.3.7-dist/css/bootstrap.min.css) }} relstylesheet link href{{ static_url(custom_css/ie10-viewport-bug-workaround.css) }} relstylesheet link href{{ static_url(custom_css/navbar-fixed-top.css) }} relstylesheet/headbodydivclasscontainerdivclasspanel panel-defaultdivclasspanel-headingchildren table/divtableclasstable table-borderedtbodytrthname/ththinfo/th/tr{% for child_info in children_infos %}trtd{{child_info[0]}}/tdtd{{child_info[1]}}/td/tr{% end %}/tbody/table/div/divscript src{{ static_url(jquery/1.12.4/jquery.min.js) }}/scriptscript src{{ static_url(bootstrap-3.3.7-dist/js/bootstrap.min.js) }}/script/body/htmlhello_tornado.py完整内容#!/usr/bin/python3# -*- coding: UTF-8 -*-importsysimportosimporttornado.httpserverimporttornado.ioloopimporttornado.optionsimporttornado.webfromtornado.templateimportTemplatedefhandler_list():return[(r/hello_tornado,hello_tornado_handler_main),]classhello_tornado_handler_main(tornado.web.RequestHandler):defget(self):self.render(htmls/hello_tornado.html,children_infos((LiLei,boy),(HanMM,girl)))if__name____main__:apptornado.web.Application(handlershandler_list(),static_pathos.path.join(os.path.dirname(__file__),htmls),debugTrue)http_servertornado.httpserver.HTTPServer(app)http_server.listen(8000)tornado.ioloop.IOLoop.instance().start()文件结构├── hello_tornado.py ├── htmls │ ├── bootstrap-3.3.7-dist │ │ ├── css │ │ │ ├── bootstrap.css │ │ │ ├── bootstrap.css.map │ │ │ ├── bootstrap.min.css │ │ │ ├── bootstrap.min.css.map │ │ │ ├── bootstrap-theme.css │ │ │ ├── bootstrap-theme.css.map │ │ │ ├── bootstrap-theme.min.css │ │ │ └── bootstrap-theme.min.css.map │ │ ├── fonts │ │ │ ├── glyphicons-halflings-regular.eot │ │ │ ├── glyphicons-halflings-regular.svg │ │ │ ├── glyphicons-halflings-regular.ttf │ │ │ ├── glyphicons-halflings-regular.woff │ │ │ └── glyphicons-halflings-regular.woff2 │ │ └── js │ │ ├── bootstrap.js │ │ ├── bootstrap.min.js │ │ └── npm.js │ ├── custom_css │ │ ├── ie10-viewport-bug-workaround.css │ │ └── navbar-fixed-top.css │ ├── hello_tornado.html │ ├── jquery └── 1.12.4 └── jquery.min.js显示如下