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
#定義一些通用的配置信息,比如數(shù)據(jù)庫的連接信息,端口信息
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__ 函數(shù)自動(dòng)調(diào)用
def __init__(self):
#這里就是url對(duì)應(yīng)的控制器,下面分別對(duì)應(yīng)一個(gè)類,來處理里面的邏輯
handlers = [
(r"/", HomeHandler),
(r"/archive", ArchiveHandler),
(r"/feed", FeedHandler),
(r"/entry/([^/]+)", EntryHandler),
(r"/compose", ComposeHandler),
(r"/auth/login", AuthLoginHandler),
(r"/auth/logout", AuthLogoutHandler),
]
#設(shè)置,如博客標(biāo)題,模板目錄,靜態(tài)文件目錄,xsrf,是否調(diào)試
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,
)
#然后調(diào)用tornado.web.Application類的__init__函數(shù)加載進(jìn)來
tornado.web.Application.__init__(self, handlers, **settings)
# Have one global connection to the blog DB across all handlers
#數(shù)據(jù)庫連接信息
self.db = torndb.Connection(
host=options.mysql_host, database=options.mysql_database,
user=options.mysql_user, password=options.mysql_password)
#基類,繼承自tornado.web.RequestHandler 的,后面的類都是繼承這個(gè)類的
class BaseHandler(tornado.web.RequestHandler):
#屬性裝飾器,使db函數(shù)變成一個(gè)屬性,便于后面直接使用
@property
def db(self):
return self.application.db
#獲得當(dāng)前的用戶
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 重定向到一個(gè)url
self.redirect("/compose")
return
#render 渲染一個(gè)模板,后面是參數(shù)
self.render("home.html", entries=entries)
class EntryHandler(BaseHandler):
def get(self, slug):
#get 得到一個(gè)值
entry = self.db.get("SELECT * FROM entries WHERE slug = %s", slug)
#raise 觸發(fā)一個(gè)錯(cuò)誤信息,后面必須接類型
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是執(zhí)行的意思
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()
#這里定義一個(gè)函數(shù),來供上面調(diào)用
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參數(shù)的值,默認(rèn)為"/"
self.redirect(self.get_argument("next", "/"))
class EntryModule(tornado.web.UIModule):
def render(self, entry):
return self.render_string("modules/entry.html", entry=entry)
#入口函數(shù)
def main():
tornado.options.parse_command_line()
#創(chuàng)建一個(gè)服務(wù)器
http_server = tornado.httpserver.HTTPServer(Application())
#監(jiān)聽端口
http_server.listen(options.port)
#啟動(dòng)服務(wù)
tornado.ioloop.IOLoop.instance().start()
#調(diào)用的入口
if __name__ == "__main__":
main()
最后總結(jié)一下:
1)tornado框架中提供的幾個(gè)demo,都是以這種形式來創(chuàng)建一個(gè)應(yīng)用的
2)對(duì)每一個(gè)控制器函數(shù),要么是,只可能有2個(gè)對(duì)外的函數(shù),一個(gè)是get,一個(gè)是post
3)數(shù)據(jù)庫有3中調(diào)用方式,query,get,exec
4)獲取參數(shù)的值使用 get_argument 函數(shù)
5)重定向用redirect 函數(shù)
6)所有的函數(shù)都是屬性這個(gè)類的,所有都用self調(diào)用
7)渲染模板用render函數(shù)
- Python的Tornado框架的異步任務(wù)與AsyncHTTPClient
- Python Web服務(wù)器Tornado使用小結(jié)
- Python(Tornado)模擬登錄小米搶手機(jī)
- tornado捕獲和處理404錯(cuò)誤的方法
- python常用web框架簡單性能測試結(jié)果分享(包含django、flask、bottle、tornado)
- 使用基于Python的Tornado框架的HTTP客戶端的教程
- 簡單介紹Python的Tornado框架中的協(xié)程異步實(shí)現(xiàn)原理
- Tornado Web服務(wù)器多進(jìn)程啟動(dòng)的2個(gè)方法
- Python的Tornado框架實(shí)現(xiàn)圖片上傳及圖片大小修改功能
相關(guān)文章
Django項(xiàng)目創(chuàng)建及管理實(shí)現(xiàn)流程詳解
這篇文章主要介紹了Django項(xiàng)目創(chuàng)建及管理實(shí)現(xiàn)流程詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-10-10Python模塊學(xué)習(xí) datetime介紹
Python提供了多個(gè)內(nèi)置模塊用于操作日期時(shí)間,像calendar,time,datetime。time模塊我在之前的文章已經(jīng)有所介紹,它提供的接口與C標(biāo)準(zhǔn)庫time.h基本一致2012-08-08python--shutil移動(dòng)文件到另一個(gè)路徑的操作
這篇文章主要介紹了python--shutil移動(dòng)文件到另一個(gè)路徑的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-07-07golang/python實(shí)現(xiàn)歸并排序?qū)嵗a
這篇文章主要給大家介紹了關(guān)于golang/python實(shí)現(xiàn)歸并排序的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08對(duì)python遍歷文件夾中的所有jpg文件的實(shí)例詳解
今天小編就為大家分享一篇對(duì)python遍歷文件夾中的所有jpg文件的實(shí)例詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-12-12python?tkinter自定義實(shí)現(xiàn)Expander控件
和其他成熟的GUI庫相比,tkinter的組件并不是太多,但在自定義組件這一點(diǎn)上,并不遜色于其他框架,下面小編就教大家如何自定義一個(gè)Expander控件吧2023-08-08Python基礎(chǔ)知識(shí)學(xué)習(xí)之類的繼承
今天帶大家學(xué)習(xí)Python的基礎(chǔ)知識(shí),文中對(duì)python類的繼承作了非常詳細(xì)的介紹,對(duì)正在學(xué)習(xí)python基礎(chǔ)的小伙伴們很有幫助,需要的朋友可以參考下2021-05-05