首页 > 代码库 > tornado 学习笔记

tornado 学习笔记

import  tornado.ioloop
import  tornado.web
class MainHanlwe(tornado.web.RequestHandler):
    def get(self):
        login_user=self.get_secure_cookie(login_user,None)
        if login_user:
            self.write(login_user)
        else:
            self.redirect(/login)
class LoginHanmder(tornado.web.RequestHandler):
    def get(self):
        # self.current_user()
        self.render(login.html,**{statue:‘‘})
    def post(self,*args,**kwargs):
        username=self.get_argument(name)
        password=self.get_argument(pwd)
        print(username,password)
        if username ==liwanlei and password ==123:
            self.set_secure_cookie(login_user,leizi)
            self.redirect(/index)
        else:
            self.render(login.html,**{status:用户名或者密码错误})
class MainHandler(tornado.web.RequestHandler):
    def get(self):
        self.render(shangchuan.html)
    def post(self,*args,**kwargs):
        file=self.request.files[fff]
        for mes in file:
            file_name = mes[filename]
            with open(file_name, wb) as up:
                up.write(mes[body])
class Maiandler(tornado.web.RequestHandler):
    def get(self):
        self.render(yan.html)
    def post(self, *args, **kwargs):
        obj = MainForm()
        result = obj.check_valid(self)
        self.write(ok)
setting={
    template_path:template,
    static_path: static,
    static_url_prefix: /static/,
    cookie_secret: aiuasdhflashjdfoiuashdfiuh,
    xsrf_cookie:True
}
application=tornado.web.Application([
    (r"/index", Maiandler),
    (r"/login", LoginHanmder),
],**setting)
if __name__==__main__:
    application.listen(5000)
    tornado.ioloop.IOLoop.instance().start()
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <title>上传文件</title>
</head>
<body>
    <form id="my_form" name="form" action="/index" method="POST"  enctype="multipart/form-data" >
        <input name="fff" id="my_file"  type="file" />
        <input type="submit" value=http://www.mamicode.com/"提交"  />
    </form>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
{%if status %}
{{status}}
{% end %}
<form action="" method="post">
    <input type="text" name="name">
    <input type="password" name="pwd">
    <input type="submit" value="http://www.mamicode.com/登录">
</form>
</body>
</html>

  

tornado 学习笔记