tornado总结2-静态文件设置
2019獨角獸企業重金招聘Python工程師標準>>>
代碼結構
main.py是程序入口
handlers目錄是用來放置路由處理類的地方,為了能讓外部能夠使用里面的東西,就把它做成了一個包,里面包含了一個空的__init__文件
static目錄是用來放js,css之類的靜態文件的
templates是用來放置html模板的地方
main.py說明
#main.py import?osimport?tornado.httpserver import?tornado.ioloop import?tornado.webfrom?handlers.home?import?HomeHandlerclass?PageNotFoundHandler(tornado.web.RequestHandler):def?get(self):return?self.write_error(404)class?Application(tornado.web.Application):def?__init__(self):handlers?=?[(r"/",?tornado.web.RedirectHandler,?{"url":?"/home"}),(r"/home",?HomeHandler),(r".*",?PageNotFoundHandler),]settings?=?dict(static_path=?os.path.join(os.path.dirname(__file__),?"static"),template_path=os.path.join(os.path.dirname(__file__),?"templates"),)tornado.web.Application.__init__(self,?handlers,?**settings)if?__name__?==?"__main__":port?=?8899application?=?Application()http_server?=?tornado.httpserver.HTTPServer(application,?xheaders=True)http_server.listen(port)print('Listen?on?http://localhost:{0}'.format(port))tornado.ioloop.IOLoop.instance().start()?
??handlers 里面設置了三個路徑??
r"/" 被重定向到了?"/home"
r"/home" 路徑的請求現在被HomeHandler來處里,這個類會在后面介紹
r".*"路徑被用來當做404的處理, 這個必須放在最后, tornado對于路徑的匹配順序是按照handlers數組順序進行匹配的
如果你把handlers的順序改成下面這個樣子,就會發現任何路徑都是404了,因為只要發現第一個匹配的就立刻進行處理
????????handlers?=?[(r".*",?PageNotFoundHandler),(r"/",?tornado.web.RedirectHandler,?{"url":?"/home"}),(r"/home",?HomeHandler),]settings 里面有兩個設置
static_path是用來設置靜態文件地址的, tornado內部監視了路徑r"/static/*"的所有請求,而這個設置是用來將這個路徑下面的所有請求,直接返回本地文件的,直接看tornado.web.py的源代碼就明白了.
以下是tornado.web.py中關于static_path設置的處理流程
#tornado?web.py #...if?self.settings.get("static_path"):path?=?self.settings["static_path"]handlers?=?list(handlers?or?[])static_url_prefix?=?settings.get("static_url_prefix","/static/")static_handler_class?=?settings.get("static_handler_class",StaticFileHandler)static_handler_args?=?settings.get("static_handler_args",?{})static_handler_args['path']?=?pathfor?pattern?in?[re.escape(static_url_prefix)?+?r"(.*)",r"/(favicon\.ico)",?r"/(robots\.txt)"]:handlers.insert(0,?(pattern,?static_handler_class,static_handler_args))直接將"/static/"映射為本地文件目錄, 因此在html里面使用js和css時一般建議的格式是以"/static/"開頭
<script?src="/static/js/home.js"></script>template_path是用來放置html模板文件的地方, 提供給?tornado.web.RequestHandler的render方法來查找模板文件用的.
home.py說明
#home.py
import?tornado.webclass?HomeHandler(tornado.web.RequestHandler):def?get(self):return?self.render('home.html') ? 對于收到的get請求,直接使用render返回"home.html", 而"home.html"的查找根目錄是在前面的template_path設置的.
? tornado的模板還有很多其他的功能,下篇再講.
其他文件說明
home.html
<!DOCTYPE?html> <html> <head><meta?charset="UTF-8"><title>主頁</title> </head> <body><h1?id="home_head">這是主頁</h1><script?src="/static/js/home.js"></script> </body> </html>home.js
window.onload?=?function?()?{alert("頁面載入彈窗"); }
實際運行效果
頁面載入時出現js設置的彈窗
這說明html里面的
<script?src="/static/js/home.js"></script>
被tornado正確返回了.
轉載于:https://my.oschina.net/u/111188/blog/670608
總結
以上是生活随笔為你收集整理的tornado总结2-静态文件设置的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 使用ENTER模拟触发表单提交或者cli
- 下一篇: 需要我们了解的SQL Server阻塞原