Web框架之Tornado

https://www.cnblogs.com/wupeiqi/articles/5702910.html

https://www.cnblogs.com/wupeiqi/p/4375610.html

 

pip3 install tornado
 
源码安装
https://pypi.python.org/packages/source/t/tornado/tornado-4.3.tar.gz
框架使用

 

import tornado.ioloop
import tornado.web

class MainHandler(tornado.web.RequestHandler):
    def get(self):
        self.write("Hello, world")

class LoginHandler(tornado.web.RequestHandler):
    def get(self):
        # self.write("请登录")
        self.render("login.html")

    def post(self, *args, **kwargs):
        v = self.get_argument('username')
        print(v)
        self.redirect('/index.html')

settings = {
    'template_path': 'templates',
    'static_path': 'static',
    'static_url_prefix': '/ppp/',
}

# application对象中封装了:路由信息,配置信息
application = tornado.web.Application([
    (r"/login.html", LoginHandler),
    (r"/index.html", MainHandler),
],**settings)

# application.add_handlers('buy.oldboy.com',[
#     (r"/login.html", LoginHandler),
#     (r"/index.html", MainHandler),
# ])


if __name__ == "__main__":
    # 创建socket对象
    # sock = socket.socket()
    # inputs = [socket,]
    application.listen(8888)

    # 开启 r,w,e = select.select(inputs,)
    tornado.ioloop.IOLoop.instance().start()

login.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h1>登录</h1>
    <form action="/login.html" method="POST">
        <input type="text" name="username" placeholder="用户名" />
        <input type="submit" value="提交" />
    </form>
    <img src="/ppp/1.png">
</body>
</html>

 

posted @ 2019-04-13 13:56  狂奔~  阅读(146)  评论(0编辑  收藏  举报