tornado多路由示例

时间:2021-10-02 00:45:21

main.py代码:

# encoding: utf-8


"""
@version: ??
@author: andu99
@contact: andux@qq.com
@site: http://www.cnblogs.com/andu99/
@software: PyCharm
@file: main.py
@time: 2018/5/9 0009 上午 11:05
"""

import tornado.web
import tornado.ioloop
import tornado.httpserver
import tornado.options

from tornado.options import define, options
from tornado.web import RequestHandler, url

# 自定义参数,下面调用这个参数http_server.listen(options.port)
define('port', type=int, default=8000, help='服务器端口')


class IndexHandler(tornado.web.RequestHandler):
    """主页处理类"""

    def get(self):
        """get请求"""
        self.write('<a href="' + self.reverse_url('a_url') + '">这是链接</a>')


class SubjectHandler(RequestHandler):
    def initialize(self, subject):
        self.subject = subject

    def get(self):
        self.write(self.subject)


if __name__ == '__main__':
    tornado.options.parse_command_line()
    app = tornado.web.Application([
        ('/', IndexHandler),
        ('/learn', SubjectHandler, {'subject': '学习python的tornado框架'}),
        url('/a', SubjectHandler, {'subject': '点击链接后的内容'}, name='a_url'),
    ], dubug=True)
    http_server = tornado.httpserver.HTTPServer(app)
    http_server.listen(options.port)
    tornado.ioloop.IOLoop.current().start()

点击运行:

tornado多路由示例

tornado多路由示例

tornado多路由示例

 

 tornado多路由示例