Tornado是一个基于python的web框架,xxxxx

 安装

python -m pip install tornado

第一个Tornado程序

 安装完毕我们就可以新建一个app.py文件,放入下面的代码直接运行就可以了,然后在浏览器访问127.0.0.1:8888

import tornado.ioloop
import tornado.web

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

application = tornado.web.Application([
    (r"/", MainHandler),
])

if __name__ == "__main__":
    application.listen(8888)
    tornado.ioloop.IOLoop.instance().start()

 tornado执行过程:

  • 第一步:执行脚本,监听 8888 端口
  • 第二步:浏览器客户端访问 /index  -->  http://127.0.0.1:8888/index
  • 第三步:服务器接受请求,并交由对应的类处理该请求
  • 第四步:类接受到请求之后,根据请求方式(post / get / delete ...)的不同调用并执行相应的方法
  • 第五步:方法返回值的字符串内容发送浏览器

路由系统

路由系统执行过程是:

用户访问一个指定url(如:www.abc.org/index)   ---->  路由系统去匹配url找到Handler  ---->  Handler处理用户请求(get/post)

路由系统其实就是 url 和 类 的对应关系,这里不同于其他框架,其他很多框架均是 url 对应 函数,Tornado中每个url对应的是一个类。

顺带提一句,Tornado自己基于socket实现Web服务,Django等需要依赖其他的wsgi

import tornado.web

settings = {
    'template_path': 'views',
    'static_path': 'static',
}


class IndexHandler(tornado.web.RequestHandler):
    def get(self,page=None):
        pass
    
    def post(self, *args, **kwargs):
        pass


application = tornado.web.Application([
    (r"/", IndexHandler),
    (r"/index/", IndexHandler),
], **settings)


if __name__ == "__main__":
    application.listen(8000)
    tornado.ioloop.IOLoop.instance().start()
View Code

相关文章:

  • 2021-09-29
  • 2021-09-29
  • 2022-12-23
  • 2022-12-23
  • 2021-11-02
  • 2021-10-09
  • 2021-10-09
猜你喜欢
  • 2021-11-02
  • 2021-11-29
  • 2021-09-03
  • 2021-05-19
  • 2022-02-08
相关资源
相似解决方案