Tornado框架 Tornado介绍 基本使用12345678910111213141516171819#!/usr/bin/env python# -*- coding:utf-8 -*- import tornado.ioloopimport tornado.web class MainHandler(tornado.web.RequestHandler): def get(self): #根据请求method,通过反射找到get方法; self.write("Hello, world") application = tornado.web.Application([ (r"/index", MainHandler), #一个映射对应一个类]) if __name__ == "__main__": application.listen
Tornado 是 FriendFeed 使用的可扩展的异步非阻塞式 web 服务器及其相关工具的开源版本。这个 Web 框架看起来有些像web.py(豆瓣用这个写的) 或者 Google 的 webapp,不过为了能有效利用非阻塞式服务器环境,这个 Web 框架还包含了一些相关的有用工具 和优化。
Tornado 和现在的主流 Web 服务器框架(包括大多数 Python 的框架)有着明显的区别:它是非阻塞式服务器,而且速度相当快。得利于其 非阻塞的方式和对 epoll 的运用,Tornado 每秒可以处理数以千计的连接,这意味着对于实时 Web 服务来说,Tornado 是一个理想的 Web 框架。我们开发这个 Web 服务器的主要目的就是为了处理 FriendFeed 的实时功能 ——在 FriendFeed 的应用里每一个活动用户都会保持着一个服务器连接。(关于如何扩容 服务器,以处理数以千计的客户端的连接的问题,请参阅 C10K problem。)
基本使用
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import
tornado.ioloop
import
tornado.web
class
MainHandler(tornado.web.RequestHandler):
def
get(
self
): #根据请求method,通过反射找到get方法;
self
.write(
"Hello, world"
)
application
=
tornado.web.Application([
(r
"/index"
, MainHandler), #一个映射对应一个类
])
if
__name__
=
=
"__main__"
:
application.listen(
8888
) #socket
tornado.ioloop.IOLoop.instance().start()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import
tornado.ioloop
import
tornado.web
class
MainHandler(tornado.web.RequestHandler):
def
get(
self
): #根据请求method,通过反射找到get方法;
self
.write(
"Hello, world"
)
application
=
tornado.web.Application([
(r
"/index"
, MainHandler), #一个映射对应一个类
])
if
__name__
=
=
"__main__"
:
application.listen(
8888
) #socket
tornado.ioloop.IOLoop.instance().start()
第一步:执行脚本,监听 8888 端口
第二步:浏览器客户端访问 /index --> http://127.0.0.1:8888/index
第三步:服务器接受请求,并交由对应的类处理该请求
第四步:类接受到请求之后,根据请求方式(post / get / delete ...)的不同调用并执行相应的方法
第五步:方法返回值的字符串内容发送浏览器
详细介绍一、路由系统
路由系统其实就是 url 和 类 的对应关系,这里不同于其他框架,其他很多框架均是 url 对应 函数,Tornado中每个url对应的是一个类。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import
tornado.ioloop
import
tornado.web
class
MainHandler(tornado.web.RequestHandler):
def
get(
self
):
self
.write(
"Hello, world"
)
class
StoryHandler(tornado.web.RequestHandler):
def
get(
self
, story_id):
self
.write(
"You requested the story "
+
story_id)
class
BuyHandler(tornado.web.RequestHandler):
def
get(
self
):
self
.write(
"buy.wupeiqi.com/index"
)
application
=
tornado.web.Application([
(r
"/index"
, MainHandler),
(r
"/story/([0-9]+)"
, StoryHandler),
])
application.add_handlers(
'buy.wupeiqi.com$'
, [
(r
'/index'
,BuyHandler),
])
if
__name__
=
=
"__main__"
:
application.listen(
80
)
tornado.ioloop.IOLoop.instance().start()
二、模板
三、实用功能
四、扩展功能
1、自定义session
2、自定义模型绑定
表单提交验证
用户输入与对象正则表达式的字段进行匹配,没错误返回True。
template/index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<!DOCTYPE html>
<
html
>
<
head
lang
=
"en"
>
<
meta
charset
=
"UTF-8"
>
<
title
></
title
>
<
link
href
=
"{{static_url("
commons.css")}}"
rel
=
"stylesheet"
/>
</
head
>
<
body
>
<
h1
>hello</
h1
>
<
form
action
=
"/index"
method
=
"post"
>
<
p
>hostname: <
input
type
=
"text"
name
=
"host"
/> </
p
>
<
p
>ip: <
input
type
=
"text"
name
=
"ip"
/> </
p
>
<
p
>port: <
input
type
=
"text"
name
=
"port"
/> </
p
>
<
p
>phone: <
input
type
=
"text"
name
=
"phone"
/> </
p
>
<
input
type
=
"submit"
/>
</
form
>
</
body
>
</
html
>
index.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import
tornado.ioloop
import
tornado.web
import
re
class
MainForm(
object
):
def
__init__(
self
):
self
.host
=
"(.*)"
self
.ip
=
"^(25[0-5]|2[0-4]d|[0-1]?d?d)(.(25[0-5]|2[0-4]d|[0-1]?d?d)){3}$"
self
.port
=
'(d+)'
self
.phone
=
'^1[3|4|5|8][0-9]d{8}$'
def
check_valid(
self
, request):
form_dict
=
self
.__dict__
#获取这个对象的所有字段
for
key, regular
in
form_dict.items():
post_value
=
request.get_argument(key)
#get_argument()是request方法,获取html表单中name就是key,所以这个字段名称和name最好对应
# 让提交的数据 和 定义的正则表达式进行匹配,只能用match,从头匹配
ret
=
re.match(regular, post_value)
if
not
ret:
return
False
return
True
class
MainHandler(tornado.web.RequestHandler):
def
get(
self
):
self
.render(
'index.html'
,name
=
"test"
)
def
post(
self
,
*
args,
*
*
kwargs):
obj
=
MainForm()
result
=
obj.check_valid(
self
)
print
result
#全部输入正确就是True
self
.write(
'ok'
)
settings
=
{
'template_path'
:
'template'
,
'static_path'
:
'static'
,
'static_url_prefix'
:
'/static/'
,
}
application
=
tornado.web.Application([
(r
"/index"
, MainHandler),
],
*
*
settings)
#路由系统,应用配置settings,静态文件会去template目录里找;
if
__name__
=
=
"__main__"
:
application.listen(
8888
)
tornado.ioloop.IOLoop.instance().start()