tornado---tornado项目结构:

tornado项目模板:

整体结构:

在这里插入图片描述

server.py

# tornado的基础web框架模块
# tornado核心io循环模块,封装了linux的epoll和kqueue,是tornado高效的基础
import tornado.web
import tornado.ioloop
import tornado.httpserver
import tornado.options
# 导入参数
from project02 import config
from project02.application import Application

if __name__ == "__main__":
    # 实例化一个app对象

    app = Application()

    # 先创建一个服务,再去绑定端口
    httpServer = tornado.httpserver.HTTPServer(app)
    # 使用变量的值
    httpServer.bind(config.options["port"])
    # 开启的进程数
    httpServer.start(1)

    # IOLoop.current():返回当前线程的IOLoop实例
    # IOLoop.start():启动IOLoop实例的I/O循环,同时开启监听
    tornado.ioloop.IOLoop.current().start()

程序的入口。

config.py:

import os

BASE_DIRS = os.path.dirname(__file__)

options = {
    "port": 11111
}

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

}

程序的一些配置文件。

application.py:

import tornado.web

from project02 import config
from project02.views.index import AAA


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

写一些url

views/index.py:

import tornado.web

# 类似于django中的视图
class AAA(tornado.web.RequestHandler):
    # 处理get请求
    def get(self, *args, **kwargs):
        # 给浏览器响应信息
        self.write("aaaaaaa")

static:

放一些静态文件

templates:

放一些模板

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章