tornado框架blog模块分析与使用
#!/usr/bin/env python
#
# copyright 2009 facebook
#
# licensed under the apache license, version 2.0 (the "license"); you may
# not use this file except in compliance with the license. you may obtain
# a copy of the license at
#
# http://www.apache.org/licenses/license-2.0
#
# unless required by applicable law or agreed to in writing, software
# distributed under the license is distributed on an "as is" basis, without
# warranties or conditions of any kind, either express or implied. see the
# license for the specific language governing permissions and limitations
# under the license.
import markdown
import os.path
import re
import torndb
import tornado.auth
import tornado.httpserver
import tornado.ioloop
import tornado.options
import tornado.web
import unicodedata
from tornado.options import define, options
#定义一些通用的配置信息,比如数据库的连接信息,端口信息
define("port", default=8888, help="run on the given port", type=int)
define("mysql_host", default="127.0.0.1:3306", help="blog database host")
define("mysql_database", default="blog", help="blog database name")
define("mysql_user", default="root", help="blog database user")
define("mysql_password", default="sa123", help="blog database password")
#定义application信息,它是继承tornado.web.application 的
class application(tornado.web.application):
# __init__ 函数自动调用
def __init__(self):
#这里就是url对应的控制器,下面分别对应一个类,来处理里面的逻辑
handlers = [
(r"/", homehandler),
(r"/archive", archivehandler),
(r"/feed", feedhandler),
(r"/entry/([^/]+)", entryhandler),
(r"/compose", composehandler),
(r"/auth/login", authloginhandler),
(r"/auth/logout", authlogouthandler),
]
#设置,如博客标题,模板目录,静态文件目录,xsrf,是否调试
settings = dict(
blog_title=u"tornado blog",
template_path=os.path.join(os.path.dirname(__file__), "templates"),
static_path=os.path.join(os.path.dirname(__file__), "static"),
ui_modules={"entry": entrymodule},
xsrf_cookies=true,
cookie_secret="__todo:_generate_your_own_random_value_here__",
login_url="/auth/login",
debug=true,
)
#然后调用tornado.web.application类的__init__函数加载进来
tornado.web.application.__init__(self, handlers, **settings)
# have one global connection to the blog db across all handlers
#数据库连接信息
self.db = torndb.connection(
host=options.mysql_host, database=options.mysql_database,
user=options.mysql_user, password=options.mysql_password)
#基类,继承自tornado.web.requesthandler 的,后面的类都是继承这个类的
class basehandler(tornado.web.requesthandler):
#属性装饰器,使db函数变成一个属性,便于后面直接使用
@property
def db(self):
return self.application.db
#获得当前的用户
def get_current_user(self):
user_id = self.get_secure_cookie("blogdemo_user")
if not user_id: return none
return self.db.get("select * from authors where id = %s", int(user_id))
#首页
class homehandler(basehandler):
def get(self):
#query 查询很多列
entries = self.db.query("select * from entries order by published "
"desc limit 5")
if not entries:
#redirect 重定向到一个url
self.redirect("/compose")
return
#render 渲染一个模板,后面是参数
self.render("home.html", entries=entries)
class entryhandler(basehandler):
def get(self, slug):
#get 得到一个值
entry = self.db.get("select * from entries where slug = %s", slug)
#raise 触发一个错误信息,后面必须接类型
if not entry: raise tornado.web.httperror(404)
self.render("entry.html", entry=entry)
class archivehandler(basehandler):
def get(self):
entries = self.db.query("select * from entries order by published "
"desc")
self.render("archive.html", entries=entries)
class feedhandler(basehandler):
def get(self):
entries = self.db.query("select * from entries order by published "
"desc limit 10")
self.set_header("content-type", "application/atom+xml")
self.render("feed.xml", entries=entries)
class composehandler(basehandler):
#装饰器
@tornado.web.authenticated
def get(self):
id = self.get_argument("id", none)
entry = none
if id:
entry = self.db.get("select * from entries where id = %s", int(id))
self.render("compose.html", entry=entry)
@tornado.web.authenticated
def post(self):
id = self.get_argument("id", none)
title = self.get_argument("title")
text = self.get_argument("markdown")
html = markdown.markdown(text)
if id:
entry = self.db.get("select * from entries where id = %s", int(id))
if not entry: raise tornado.web.httperror(404)
slug = entry.slug
#execute是执行的意思
self.db.execute(
"update entries set title = %s, markdown = %s, html = %s "
"where id = %s", title, text, html, int(id))
else:
slug = unicodedata.normalize("nfkd", title).encode(
"ascii", "ignore")
slug = re.sub(r"[^\w]+", " ", slug)
slug = "-".join(slug.lower().strip().split())
if not slug: slug = "entry"
while true:
e = self.db.get("select * from entries where slug = %s", slug)
if not e: break
slug += "-2"
self.db.execute(
"insert into entries (author_id,title,slug,markdown,html,"
"published) values (%s,%s,%s,%s,%s,utc_timestamp())",
self.current_user.id, title, slug, text, html)
self.redirect("/entry/" + slug)
class authloginhandler(basehandler, tornado.auth.googlemixin):
@tornado.web.asynchronous
def get(self):
if self.get_argument("openid.mode", none):
self.get_authenticated_user(self.async_callback(self._on_auth))
return
self.authenticate_redirect()
#这里定义一个函数,来供上面调用
def _on_auth(self, user):
if not user:
raise tornado.web.httperror(500, "google auth failed")
author = self.db.get("select * from authors where email = %s",
user["email"])
if not author:
# auto-create first author
any_author = self.db.get("select * from authors limit 1")
if not any_author:
author_id = self.db.execute(
"insert into authors (email,name) values (%s,%s)",
user["email"], user["name"])
else:
self.redirect("/")
return
else:
author_id = author["id"]
self.set_secure_cookie("blogdemo_user", str(author_id))
self.redirect(self.get_argument("next", "/"))
class authlogouthandler(basehandler):
def get(self):
self.clear_cookie("blogdemo_user")
#get_argument为获得next参数的值,默认为"/"
self.redirect(self.get_argument("next", "/"))
class entrymodule(tornado.web.uimodule):
def render(self, entry):
return self.render_string("modules/entry.html", entry=entry)
#入口函数
def main():
tornado.options.parse_command_line()
#创建一个服务器
http_server = tornado.httpserver.httpserver(application())
#监听端口
http_server.listen(options.port)
#启动服务
tornado.ioloop.ioloop.instance().start()
#调用的入口
if __name__ == "__main__":
main()
最后总结一下:
1)tornado框架中提供的几个demo,都是以这种形式来创建一个应用的
2)对每一个控制器函数,要么是,只可能有2个对外的函数,一个是get,一个是post
3)数据库有3中调用方式,query,get,exec
4)获取参数的值使用 get_argument 函数
5)重定向用redirect 函数
6)所有的函数都是属性这个类的,所有都用self调用
7)渲染模板用render函数
上一篇: 硬盘,CPU疑难问答
推荐阅读