美文网首页
Tornado 项目基础架构

Tornado 项目基础架构

作者: 深吸一口气 | 来源:发表于2021-05-26 20:58 被阅读0次
base_project/
--|application/
----|__init__.py
----|config.py
----|views/
------|index.py
----|templates/
----|static/
--|server.py

application/config.py

import os

BASE_DIR = os.path.dirname(__file__)

options = {
    "port": 8000
}


settings = {
    "debug": True,
    "template_path": os.path.join(BASE_DIR, "templates"),
    "static_path": os.path.join(BASE_DIR, "static")
}

application/views/index.py

import tornado.web


class IndexHandler(tornado.web.RequestHandler):
    def get(self):
        self.write("hello index")

application/__init__.py

import tornado.web
import tornado.ioloop
from application import config
from application.views import index


class Application(tornado.web.Application):
    def __init__(self):
        handlers = [
            (r"/", index.IndexHandler)
        ]
        super(Application, self).__init__(handlers, **config.settings)

server.py

from application import Application
from application import config

import tornado.ioloop


if __name__ == "__main__":
    app = Application()
    app.listen(config.options["port"])
    tornado.ioloop.IOLoop.current().start()

启动项目

python server.py

浏览器访问http://127.0.0.1:8000即可访问项目

相关文章

网友评论

      本文标题:Tornado 项目基础架构

      本文链接:https://www.haomeiwen.com/subject/ykrlsltx.html