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:

放一些模板

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