參考:https://www.cnblogs.com/sunshuhai/articles/6253815.htmljavascript
1、代碼目錄構建css
代碼目錄設置以下圖:html
#!/usr/bin/env python # -*- coding:utf-8 -*- import tornado.web class LogInHandler(tornado.web.RequestHandler): def get(self): self.write("LogIn") class LogOutHandler(tornado.web.RequestHandler): def get(self): self.write("LogOut") class RegistHandler(tornado.web.RequestHandler): def get(self): self.write("Regist")
#!/usr/bin/env python # -*- coding:utf-8 -*- import tornado.web class IndexHandler(tornado.web.RequestHandler): def get(self): self.write("Index")
#!/usr/bin/env python # -*- coding:utf-8 -*- import tornado.ioloop import tornado.web from controllers import account from controllers import home settings = { 'template_path':'views', #配置模板文件路徑 'static_path':'statics', #配置靜態文件路徑 } #路由映射 application = tornado.web.Application([ (r"/login", account.LogInHandler), (r"/logout", account.LogOutHandler), (r"/regist", account.RegistHandler), (r"/index", home.IndexHandler) ],**settings) #**settings是讓配置生效 if __name__ == "__main__": application.listen(8888) tornado.ioloop.IOLoop.instance().start()
2、基於正則的動態路由java
(r"/index/(\d+)", home.IndexHandler)
class IndexHandler(tornado.web.RequestHandler):
def get(self,num):
print(num)
self.write("Index")
http://127.0.0.1:8888/index/11,用這個url訪問時會打印輸出:11python
(r"/index/(\d+)/(\d*)", home.IndexHandler)
class IndexHandler(tornado.web.RequestHandler):
def get(self,num,page):
print(num,page)
self.write("Index")
http://127.0.0.1:8888/index/11/22,用這個url訪問時會打印輸出:11,22web
(r"/index/(?P<page>\d+)/(?P<num>\d*)", home.IndexHandler) #?P<page>匹配到的第一個分組命名爲page
class IndexHandler(tornado.web.RequestHandler):
def get(self,num,page):
print(num,page)
self.write("Index")
http://127.0.0.1:8888/index/11/22,用這個url訪問時會打印輸出:22,11app
(三)XSS跨站腳本攻擊框架
以下面這個htmlssh
<h1>aaaaa<h1>
<script>alert(123)</script>ide
這個html一旦被訪問就會運行script代碼,彈窗顯示123
那麼在一些網站中的文本框中能夠輸入script代碼,一旦網站沒有對代碼作相應的處理,就會運行
tornado是作了處理的,文本框中輸入代碼是做爲文本的,但也能夠原始狀態展現,這樣就會運行這段代碼,例如:
<td>{% raw item["email"] %}</td>
這樣一旦添加數據後,代碼會被執行,會直接彈窗顯示123
4、分頁
其中home.py中的class Page類能夠直接當作插件用,可使用在其餘的web框架中
#!/usr/bin/env python # -*- coding:utf-8 -*- import tornado.web class LogInHandler(tornado.web.RequestHandler): def get(self): self.write("LogIn") class LogOutHandler(tornado.web.RequestHandler): def get(self): self.write("LogOut") class RegistHandler(tornado.web.RequestHandler): def get(self): self.write("Regist")
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>分頁</title> <style> .page a{ display: inline-block; padding: 5px; margin:3px; background-color: darkseagreen; } .page a.ActivePage{ background-color: brown; color: aliceblue; } </style> </head> <body> <form method="post" action="/index/"> 用戶名:<input type="text" name="username"> 郵箱:<input type="text" name="email"> <input type="submit" value="提交"> </form> <table> <thead> <tr> <th>用戶名</th> <th>郵箱</th> </tr> </thead> <tbody> {% for item in list_info %} <tr> <td>{{item["username"]}}</td> <!--<td>{{item["email"]}}</td>--> <td>{% raw item["email"] %}</td> </tr> {% end %} </tbody> </table> <div class="page"> {% raw html_a %} </div> </body> </html>
#!/usr/bin/env python # -*- coding:utf-8 -*- import tornado.ioloop import tornado.web from controllers import account from controllers import home settings = { 'template_path':'views', #配置模板文件路徑 'static_path':'statics', #配置靜態文件路徑 } #路由映射 application = tornado.web.Application([ (r"/login", account.LogInHandler), (r"/logout", account.LogOutHandler), (r"/regist", account.RegistHandler), (r"/index/(?P<page>\d*)", home.IndexHandler) ],**settings) #**settings是讓配置生效 if __name__ == "__main__": application.listen(8888) tornado.ioloop.IOLoop.instance().start()
#!/usr/bin/env python # -*- coding:utf-8 -*- import tornado.web import math LIST_INFO = [] for i in range(999): LIST_INFO.append({"username":"sunshuhai" + str(i),"email": str(i) + "123.qq.com"}) class Page: #分頁的內容-列表,當前頁碼,每頁的數據條數,基礎url def __init__(self,list_content,crrent_page,num_of_page,base_url): try: crrent_page = int(crrent_page) except: crrent_page = 1 if crrent_page<1: crrent_page = 1 quotient,remainder =divmod(len(list_content),num_of_page) if remainder>0: all_page=quotient+1 else: all_page =quotient self.all_page = all_page self.crrent_page = crrent_page self.num_of_page = num_of_page self.base_url = base_url @property def start(self): return (self.crrent_page - 1) * self.num_of_page @property def end(self): return self.crrent_page * self.num_of_page def crrent_page_content(self): pass def show_page_html(self): list_html=[] if self.all_page <= 11: start_page = 1 end_page = self.crrent_page else: if self.crrent_page<=6: start_page = 1 end_page = 11 else: if self.crrent_page+5 >self.all_page: start_page = self.all_page-10 end_page = self.all_page else: start_page = self.crrent_page - 5 end_page = self.crrent_page + 5 befor = '<a href="%s1">首頁</a>' % (self.base_url) list_html.append(befor) if self.crrent_page-1>=1: next_page = '<a href="%s%s">上一頁</a>' % (self.base_url,self.crrent_page-1) else: next_page = '<a href="javascript:void(0)">上一頁</a>' list_html.append(next_page) for p in range(start_page,end_page+1): if p==self.crrent_page: list_html.append('<a class="ActivePage" href="%s%s">%s</a>' % (self.base_url,p, p)) else: list_html.append('<a href="%s%s">%s</a>' %(self.base_url,p,p)) if self.crrent_page >= self.all_page: previous_page = '<a href="javascript:void(0)">下一頁</a>' else: previous_page = '<a href="%s%s">下一頁</a>' % (self.base_url,self.crrent_page+1) list_html.append(previous_page) after ='<a href="%s%s">尾頁</a>' %(self.base_url,self.all_page) list_html.append(after) jump = """<input type="text"><a onclick="Jump('%s',this);">GO</a>""" %(self.base_url) javasc = """ <script> function Jump(baseUrl,ths) { var val = ths.previousElementSibling.value; if(val.trim().length>0){ location.href=baseUrl + val; } } </script> """ list_html.append(jump) list_html.append(javasc) html_str = ''.join(list_html) return html_str class IndexHandler(tornado.web.RequestHandler): def get(self,page): #每5條數據分一頁[0:5] [5:10] [10:15] page = Page(LIST_INFO,page,20,"/index/") list_page = LIST_INFO[page.start:page.end] html_str =page.show_page_html() self.render("index.html",list_info = list_page,html_a = html_str) def post(self, *args, **kwargs): user = self.get_argument("username") email = self.get_argument("email") temp_dict = {"username":user,"email":email} LIST_INFO.append(temp_dict) p = math.ceil(len(LIST_INFO)/5) self.redirect("/index/%s" %p)
5、路由系統之二級域名支持
1)首先配置host文件模擬域名訪問
127.0.0.1 localhost
127.0.0.1 ssh.com
127.0.0.1 buy.ssh.com
2)配置路由系統
#路由映射
application = tornado.web.Application([
(r"/login", account.LogInHandler),
(r"/logout", account.LogOutHandler),
(r"/regist", account.RegistHandler),
(r"/index/(?P<page>\d*)", home.IndexHandler)
],**settings) #**settings是讓配置生效
application.add_handlers('buy.ssh.com$',[
(r"/index/(?P<page>\d*)",buy.IndexHandler)
])
buy.py中的IndexHandler爲:
import tornado.web
class IndexHandler(tornado.web.RequestHandler):
def get(self,page):
self.write("buy.Index")
3)用域名訪問:http://buy.ssh.com:8888/index/1
結果顯示:
6、模板引擎的補充用法(繼承:extends,導入:include)
1)extends 繼承
母版的html中可使用佔位符
<div calss="pg-content">
{% block body %}{% end %}
</div>
若是繼承上面母版中有佔位符的html,只須要替換佔位符的位置就能夠
{% extends '../master/layout.html' %}
{% block boy %}
<h1>Index</h1>
{% end %}
繼承母版的優勢:公共樣式均可以放在母版中,個別樣式繼承母版後進行替換
2)include 導入
一些共用的小的組件,能夠設置爲共用,html若是想用只須要導入便可
下面是form.html中的共用代碼:
<form action="/">
<input type="text">
<input type="submit">
</form>
使用時只須要導入便可,實際就至關於在導入的位置寫了form標籤的代碼。
{% block body %}
<h1>Index</h1>
{% include '../include/form.html' %}
{% end %}
3)實例以下:
#!/usr/bin/env python # -*- coding:utf-8 -*- import tornado.web class IndexHandler(tornado.web.RequestHandler): def get(self,*args,**kwargs): self.render('extend/index.html',info_list=['111','222','333']) class FuckOffHandler(tornado.web.RequestHandler): def get(self,*args,**kwargs): self.render('extend/fuck.html')
{% extends '../master/layout.html' %} {% block body %} <h1>fuckoff</h1> {% end %} {% block js %} <script> cosole.log('fuckoff') </script> {% end %}
{% extends '../master/layout.html' %} {% block css %} <style> div{ border:1px solid red; } </style> {% end %} {% block body %} <h1>Index</h1> {% include '../include/form.html' %} {% end %} {% block js %} <script> cosole.log('index') </script> {% end %}
<form action="/"> <input type="text"> <input type="submit"> </form> <ul> {% for item in info_list %} <li>{{item}}</li> {% end %} </ul>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> .pg-header{ height: 48px; background-color: black; } .pg-footer{ height: 100px; background-color: #dddddd; } </style> <!--block塊上面是共用樣式,下面是具體每一個頁面的自定製樣式--> {% block css %}{% end %} </head> <body> <div class="pg-header"> </div> <div calss="pg-content"> {% block body %}{% end %} </div> <div class="pg-footer"> 我是頁面底部 </div> <script src="xxxxj"></script> <!--block塊上面是共用js,下面是具體每一個頁面的自定製js--> {% block js %}{% end %} </body> </html>
#!/usr/bin/env python # -*- coding:utf-8 -*- import tornado.ioloop import tornado.web from controllers import extend settings = { 'template_path':'views', #配置模板文件路徑 'static_path':'statics', #配置靜態文件路徑 } #路由映射 application = tornado.web.Application([ (r"/index", extend.IndexHandler), (r"/fuckoff", extend.FuckOffHandler), ],**settings) #**settings是讓配置生效 #二級域名的配置 # application.add_handlers('buy.ssh.com$',[ # (r"/index/(?P<page>\d*)",buy.IndexHandler) # ]) if __name__ == "__main__": application.listen(8888) tornado.ioloop.IOLoop.instance().start()