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:

放一些模板

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值