Tornado是一个轻量级完整的web框架,在Linux系统下它会使用epoll,是一个异步非阻塞的web服务器框架,对于实时应用来说很理想,想想同是异步非阻塞的nginx的残暴程度就知道了

1.路由系统

 application = tornado.web.Application([
(r'/login', LoginHandler),
(r'/index', IndexHandler),    每个url是由一个类处理的
], **settings) class IndexHandler(SessionHandler, tornado.web.RequestHandler):
def get(self):
current_user = self.session['username']
if current_user:
self.write(current_user)
else:
self.write('喵喵喵喵?')
 application = tornado.web.Application([
(r'/login', LoginHandler),
(r'/index', IndexHandler), # 处理www.bfmq.com/index
], **settings) # 二级域名
application.add_handlers('cmdb.bfmq.com', [
(r'/main', CmdbHandler),
(r'/main', MainHandler),
(r'/index', IndexHandler), # 处理cmdb.bfmq.com/index
])

2.模版引擎

与django类似,支持母板,导入,{{  }},{%  %}

 settings = {
'template_path': 'views', # 存放html模版的路径
'ui_methods': methods, # UIMethod的py文件
'ui_modules': modules, # UIModule的py文件
} class MainHandler(SessionHandler, tornado.web.RequestHandler):
def get(self):
self.render('main.html', li=[11, 22, 33, 44])
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>main</h1>
{{ miao(1) }}
{% for i in range(len(li)) %}
<p>{{ i }} --- {{ li[i] }}</p>
{% end %}
</body>
</html>
 def miao(self, a):
return '喵喵喵喵'

3.配置文件

 settings = {
'template_path': 'views', # 存放html模版的路径
'ui_methods': methods, # UIMethod的py文件
'ui_modules': modules, # UIModule的py文件
'static_path': 'statics', # 静态文件存放路径
'static_url_prefix': '/statics/', # 静态文件访问url
}

4.cookie及加密

 class IndexHandler(tornado.web.RequestHandler):
def get(self):
if not self.get_cookie("mycookie"): # 获取cookie
self.set_cookie("mycookie", "myvalue") # 设置cookie
self.write("喵喵喵喵?")
else:
self.write("喵~") class IndexHandler(tornado.web.RequestHandler):
def get(self):
if not self.get_secure_cookie("mycookie"): # 获取加密cookie
self.set_secure_cookie("mycookie", "myvalue") # 设置加密cookie
self.write("喵喵喵喵?")
else:
self.write("喵~") application = tornado.web.Application([
(r"/index", IndexHandler),
], cookie_secret="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx") # 对此应用时提供一个密钥

5.异步非阻塞

使用装饰器 + Future实现

 class AsyncHandler(tornado.web.RequestHandler):
@gen.coroutine
def get(self):
future = Future()
future.add_done_callback(self.doing)
yield future
# 或
# tornado.ioloop.IOLoop.current().add_future(future,self.doing)
# yield future def doing(self, *args, **kwargs):
self.write('async')
self.finish()

python第二十三天-----Tornado的更多相关文章

  1. 孤荷凌寒自学python第二十三天python类的封装

    孤荷凌寒自学python第二十三天python类的封装 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) 一.从怎么样访问类的内部代码块中定义的私有属性说起 类中定义的私有属性和私有方法是无法被 ...

  2. 小甲鱼Python第二十三讲课后习题--025,字典

    笔记: 1.字典是Python中唯一的映射类型 2.字典包含两个要素:键(key)和值(value)他们是成对出现的,用大括号括起来,多对存在时用逗号隔开. 3.可以用dict()直接创建字典,如di ...

  3. python第二十三天-----作业中

    #!usr/bin/env python #-*-coding:utf-8-*- # Author calmyan import os ,sys,time from core import trans ...

  4. python第二十三课——dict中的函数

    dic1 = {...} dic2 = {...} dic1.update(dic2) 1.update(dict):dic1调用update传入dic2,如果dic2中的内容在dic1中不存在,那么 ...

  5. Python开发【第二十三篇】:持续更新中...

    Python开发[第二十三篇]:持续更新中...

  6. python web框架之Tornado

    说Tornado之前分享几个前端不错的网站: -- Bootstrap http://www.bootcss.com/ -- Font Awesome http://fontawesome.io/ - ...

  7. Python第十三天 django 1.6 导入模板 定义数据模型 访问数据库 GET和POST方法 SimpleCMDB项目 urllib模块 urllib2模块 httplib模块 django和web服务器整合 wsgi模块 gunicorn模块

    Python第十三天   django 1.6   导入模板   定义数据模型   访问数据库   GET和POST方法    SimpleCMDB项目   urllib模块   urllib2模块 ...

  8. Python第二天 变量 运算符与表达式 input()与raw_input()区别 字符编码 python转义符 字符串格式化 format函数字符串格式化 帮助

    Python第二天  变量  运算符与表达式  input()与raw_input()区别  字符编码  python转义符  字符串格式化  format函数字符串格式化  帮助 目录 Pychar ...

  9. Python(九)Tornado web 框架

    一.简介 Tornado 是 FriendFeed 使用的可扩展的非阻塞式 web 服务器及其相关工具的开源版本.这个 Web 框架看起来有些像web.py 或者 Google 的 webapp,不过 ...

随机推荐

  1. 让iOS项目允许使用http协议请求

    苹果官方已经默认不让开发者使用不安全的http通信协议了,而是建议开发者使用安全的https协议.若我们还是需要使用http协议可以这样配置XCode: 1.打开info.plist文件 2.点击加号 ...

  2. 异常:没有找到本地方法库,java.lang.UnsatisfiedLinkError: no trsbean in java.library.path

    1.问题描述 迁移环境中遇到这个问题 : Fri Apr 20 15:22:31 CST 2018, Exception:500004___-500004,没有找到本地方法库,java.lang.Un ...

  3. Codeforces Round #285 (Div. 2) A, B , C 水, map ,拓扑

    A. Contest time limit per test 1 second memory limit per test 256 megabytes input standard input out ...

  4. (转)OpenStack Kilo 版本中 Neutron 的新变化

    OpenStack Kilo 版本,OpenStack 这个开源项目的第11个版本,已经于2015年4月正式发布了.现在是个合适的时间来看看这个版本中Neutron到底发生了哪些变化了,以及引入了哪些 ...

  5. dp2--合并石子(一)

    dp2--合并石子(一) 一.心得 二.题目 石子合并(一) 时间限制:1000 ms  |  内存限制:65535 KB 难度:3   描述     有N堆石子排成一排,每堆石子有一定的数量.现要将 ...

  6. Gridview中Datakeys 通过主键取得各列的值。

    首先在初始化Gridview时候定义主键的数组. GridViewTeacherStudent.DataKeyNames=new string[] {"courseId",&quo ...

  7. pandas 中文快速查询手册

    本文翻译自文章: Pandas Cheat Sheet - Python for Data Science ,同时添加了部分注解. 对于数据科学家,无论是数据分析还是数据挖掘来说,Pandas是一个非 ...

  8. mysql查询哪张表数据最大

    转载:https://blog.csdn.net/qq13650793239/article/details/81142134 mysql数据库中information_schema 数据库存储了数据 ...

  9. 怎么样修改win7下的host文件

    由于在访问tensorflow官网时访问不了,需要修改hosts文件,然而win7下因为权限问题导致不能修改hosts文件,解决方法如下: 1.先复制hosts文件到别的地方,修改完后再覆盖回来.中间 ...

  10. 清新大气的ListView下拉上拉刷新--第三方开源--PullDownListView

    下载地址:https://github.com/guojunyi/PullDownListView 使用: xml: <com.pulldownlistview.PullDownListView ...