tornado总结2-静态文件设置
2019獨(dú)角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>
代碼結(jié)構(gòu)
main.py是程序入口
handlers目錄是用來(lái)放置路由處理類(lèi)的地方,為了能讓外部能夠使用里面的東西,就把它做成了一個(gè)包,里面包含了一個(gè)空的__init__文件
static目錄是用來(lái)放js,css之類(lèi)的靜態(tài)文件的
templates是用來(lái)放置html模板的地方
main.py說(shuō)明
#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 里面設(shè)置了三個(gè)路徑??
r"/" 被重定向到了?"/home"
r"/home" 路徑的請(qǐng)求現(xiàn)在被HomeHandler來(lái)處里,這個(gè)類(lèi)會(huì)在后面介紹
r".*"路徑被用來(lái)當(dāng)做404的處理, 這個(gè)必須放在最后, tornado對(duì)于路徑的匹配順序是按照handlers數(shù)組順序進(jìn)行匹配的
如果你把handlers的順序改成下面這個(gè)樣子,就會(huì)發(fā)現(xiàn)任何路徑都是404了,因?yàn)橹灰l(fā)現(xiàn)第一個(gè)匹配的就立刻進(jìn)行處理
????????handlers?=?[(r".*",?PageNotFoundHandler),(r"/",?tornado.web.RedirectHandler,?{"url":?"/home"}),(r"/home",?HomeHandler),]settings 里面有兩個(gè)設(shè)置
static_path是用來(lái)設(shè)置靜態(tài)文件地址的, tornado內(nèi)部監(jiān)視了路徑r"/static/*"的所有請(qǐng)求,而這個(gè)設(shè)置是用來(lái)將這個(gè)路徑下面的所有請(qǐng)求,直接返回本地文件的,直接看tornado.web.py的源代碼就明白了.
以下是tornado.web.py中關(guān)于static_path設(shè)置的處理流程
#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時(shí)一般建議的格式是以"/static/"開(kāi)頭
<script?src="/static/js/home.js"></script>template_path是用來(lái)放置html模板文件的地方, 提供給?tornado.web.RequestHandler的render方法來(lái)查找模板文件用的.
home.py說(shuō)明
#home.py
import?tornado.webclass?HomeHandler(tornado.web.RequestHandler):def?get(self):return?self.render('home.html') ? 對(duì)于收到的get請(qǐng)求,直接使用render返回"home.html", 而"home.html"的查找根目錄是在前面的template_path設(shè)置的.
? tornado的模板還有很多其他的功能,下篇再講.
其他文件說(shuō)明
home.html
<!DOCTYPE?html> <html> <head><meta?charset="UTF-8"><title>主頁(yè)</title> </head> <body><h1?id="home_head">這是主頁(yè)</h1><script?src="/static/js/home.js"></script> </body> </html>home.js
window.onload?=?function?()?{alert("頁(yè)面載入彈窗"); }
實(shí)際運(yùn)行效果
頁(yè)面載入時(shí)出現(xiàn)js設(shè)置的彈窗
這說(shuō)明html里面的
<script?src="/static/js/home.js"></script>
被tornado正確返回了.
轉(zhuǎn)載于:https://my.oschina.net/u/111188/blog/670608
總結(jié)
以上是生活随笔為你收集整理的tornado总结2-静态文件设置的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 使用ENTER模拟触发表单提交或者cli
- 下一篇: 需要我们了解的SQL Server阻塞原