python第二十三天-----Tornado
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的更多相关文章
- 孤荷凌寒自学python第二十三天python类的封装
孤荷凌寒自学python第二十三天python类的封装 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) 一.从怎么样访问类的内部代码块中定义的私有属性说起 类中定义的私有属性和私有方法是无法被 ...
- 小甲鱼Python第二十三讲课后习题--025,字典
笔记: 1.字典是Python中唯一的映射类型 2.字典包含两个要素:键(key)和值(value)他们是成对出现的,用大括号括起来,多对存在时用逗号隔开. 3.可以用dict()直接创建字典,如di ...
- python第二十三天-----作业中
#!usr/bin/env python #-*-coding:utf-8-*- # Author calmyan import os ,sys,time from core import trans ...
- python第二十三课——dict中的函数
dic1 = {...} dic2 = {...} dic1.update(dic2) 1.update(dict):dic1调用update传入dic2,如果dic2中的内容在dic1中不存在,那么 ...
- Python开发【第二十三篇】:持续更新中...
Python开发[第二十三篇]:持续更新中...
- python web框架之Tornado
说Tornado之前分享几个前端不错的网站: -- Bootstrap http://www.bootcss.com/ -- Font Awesome http://fontawesome.io/ - ...
- Python第十三天 django 1.6 导入模板 定义数据模型 访问数据库 GET和POST方法 SimpleCMDB项目 urllib模块 urllib2模块 httplib模块 django和web服务器整合 wsgi模块 gunicorn模块
Python第十三天 django 1.6 导入模板 定义数据模型 访问数据库 GET和POST方法 SimpleCMDB项目 urllib模块 urllib2模块 ...
- Python第二天 变量 运算符与表达式 input()与raw_input()区别 字符编码 python转义符 字符串格式化 format函数字符串格式化 帮助
Python第二天 变量 运算符与表达式 input()与raw_input()区别 字符编码 python转义符 字符串格式化 format函数字符串格式化 帮助 目录 Pychar ...
- Python(九)Tornado web 框架
一.简介 Tornado 是 FriendFeed 使用的可扩展的非阻塞式 web 服务器及其相关工具的开源版本.这个 Web 框架看起来有些像web.py 或者 Google 的 webapp,不过 ...
随机推荐
- SQL Server technical bulletin - How to resolve a deadlock
https://support.microsoft.com/en-us/help/832524/sql-server-technical-bulletin-how-to-resolve-a-deadl ...
- vs2010下创建webservice ----第一天(建立项目,以及不连数据库进行加减乘除)
Visual Studio 2010默认采用的框架为.NET Framework4,在这个框架中已找不到直接创建WebService的模板方式了.但VS2010可以创建WebService是毋庸置疑的 ...
- 获取本机MSSQL保存凭证
首先要感谢哥们对我的指点,多谢. 当我们遇到类似情况下,如何获取保存在MSSQL工具里的凭证呢? //如果对方连接地址后面加了IP\sqlexpress 连接的时候你也记得加上,不然即使密码正确,也 ...
- Yum 命令语法
一.Yum 命令格式: yum <options> <command><package...> 二.参数说明 1.options: 1)-y:如果在工作过程中如要使 ...
- Java编程思想 Random(47)
Random类包含两个构造方法,下面依次进行介绍:1. public Random()该构造方法使用一个和当前系统时间对应的相对时间有关的数字作为种子数,然后使用这个种子数构造Random对象.2. ...
- MySQL数据copy
摘自http://database.51cto.com/art/201011/234776.htm 1. 下面这个语句会拷贝表结构到新表newadmin中. (不会拷贝表中的数据) CREATE TA ...
- Python爬虫之编写一个可复用的下载模块
看用python写网络爬虫第一课之编写可复用的下载模块的视频,发现和<用Python写网络爬虫>一书很像,写了点笔记: #-*-coding:utf-8-*- import urllib2 ...
- C# 6.0 (VS2015 CTP6)
/* C# 6.0 demo https://github.com/dotnet/roslyn/wiki/Languages-features-in-C%23-6-and-VB-14 */ using ...
- java常用封装方法
public Map<String,String> getDateByStr(String str,String startDate,String endTime){ Map<Str ...
- uva1636 - Headshot(条件概率)
简单的条件概率题,直接再来一枪没子弹的概率是所有子串”00“的数目除以‘0’的数目,随机转一下再打没子弹的概率是‘0’的数目除以总数目. #include<iostream> #inclu ...