该小程序的文件有 : 在同一目录下有 『
static 目录 之下有 images 目录
templates 目录
userStaticTemplateReload.py 文件
』
Python tornado
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import tornado.httpserver #服务于 web 模块的 http服务器, 目前没有用到
import tornado.ioloop
import tornado.web
import tornado.autoreload
import os
class BaseHandler(tornado.web.RequestHandler):
def get_current_user(self):
return self.get_secure_cookie("user")
class MainHandler(BaseHandler):
@tornado.web.authenticated
def get(self):
name = tornado.escape.xhtml_escape(self.current_user)
#self.write("Hello, " + name)
items = [(1, 2), (3, 4), (5, 6), (7, 8), (9, 10), (11, 12)]
self.render("templ.html", title = "welcome to my little station", items = items)
class LoginHandler(BaseHandler):
def get(self):
self.write('<html><body><form action="/login-do" method="post">'
'Name: <input type="text" name="name">'
'<input type="submit" value="Sign in">'
'</form></body></html>')
def post(self):
self.set_secure_cookie("user", self.get_argument("name"))
self.redirect("/")
settings = {
"static_path": os.path.join(os.path.dirname(__file__), "static"),
"template_path": os.path.join(os.path.dirname(__file__), "templates"),
"cookie_secret": "61oETzKXQAGaYdkL5gEmGeJJFuYh7EQnp2XdTP1o/Vo=",
"login_url": "/login-do",
"debug": True,
}
application = tornado.web.Application([
(r"/", MainHandler),
(r"/login-do", LoginHandler),
(r"/(foo\.jpg)", tornado.web.StaticFileHandler, dict(path=settings['static_path'])),
], **settings)
if __name__ == "__main__":
application.listen(8888)
tornado.ioloop.IOLoop.instance().start()