简单使用tornado服务器,建立hello world页面及tornado简单页面,并配置template和static文件路径,url分发

首先安装tornado:我用的最新版5.1.1

cmd 输入命令 pip install tornado

建立hello world页面及tornado简单页面,并配置template和static文件路径,url分发:

这里要注意:py文件名不能用tornado,import时会报错!

import tornado.ioloop
import tornado.web

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

#自己扩展url指向的类,调用其对应方法post/get
class IndexHandler(tornado.web.RequestHandler):
    def get(self):
        self.render("index.html")
    def post(self, *args, **kwargs):
        self.write("post request")

#配置文件
settings={
    "template_path":"template",
    "static_path":"static",
    #static文件设置别名
    "static_url_prefix":"/ray/",
}

#尾部**settings为加载配置文件
def make_app():
    return tornado.web.Application([
        (r"/", MainHandler),
        (r"/index.html", IndexHandler),
    ],**settings)

if __name__ == "__main__":
    app = make_app()
    app.listen(8888)
    tornado.ioloop.IOLoop.current().start()

index.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>index</title>
</head>
<body>
    <h1>这是个一个主页</h1>
    <form action="/index.html" method="post">
        <p> username:<input type = "text"></p>
        <p> password:<input type = "password"></p>
        <p> <input type = "submit" value="提交"></p>
    </form>
    <img src="/ray/123.jpg" />
    <!--<img src="/static/123.jpg" />-->
</body>
</html>

浏览器效果:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值