Tornadao—输出

  • write(chunk)


    将chunk数据写到输出缓冲区。我们可以像写⽂件⼀样多次使⽤write⽅法不断
    追加响应内容,最终所有写到缓冲区的内容⼀起作为本次请求的响应输出。
    如果chunk是字典,write⽅法可以将其序列化为字符串
    
    class IndexHandler(RequestHandler):
     def get(self):
     self.write("hello itcast 1!")
     self.write("hello itcast 2!")
     self.write("hello itcast 3!")
     
    class IndexHandler(RequestHandler):
     def get(self):
        stu = {
     "name":"zhangsan",
     "age":24,
     "gender":1,
     }
     self.write(stu)

    import tornado.web
    import tornado.ioloop
    import tornado.options
    from tornado.web import url
    
    from settings import params
    
    
    # 定义接收参数的名称和类型,缺省值
    tornado.options.define('p',default=9090,type=int)
    
    class IndexHandler(tornado.web.RequestHandler):
        def get(self):
            self.write("不到长城非好汉")
    
    
    class ShowHandler(tornado.web.RequestHandler):
        # 接收路由中的附加参数,参数个数和initialize中参数一致
        def initialize(self,name,age):
            self.name = name
            self.age = age
        # def initialize(self,**kwargs):
        #     self.name = kwargs.get('name')
        #     self.age = kwargs.get('age')
    
        def get(self):
            self.write("姓名:{}".format(self.name))
    
    
    class ListHandler(tornado.web.RequestHandler):
        def get(self):
            self.write("路由")
    
    
    class DetailHandler(tornado.web.RequestHandler):
        def get(self,aid):
            print(aid,type(aid))
            self.write("详细信息"+aid)
    
    
    def main():
        # 接收命名行参数
        tornado.options.parse_command_line()
        # 路由的写法
        app = tornado.web.Application(
            [(r'/',IndexHandler),
             (r'/show/',ShowHandler,{'name':'admin','age':20}),
             url(r'/list/',ListHandler,name='list'),
             (r'/detail/(\d+)/',DetailHandler),
             ],
            **params
        )
        app.listen(tornado.options.options.p)
        tornado.ioloop.IOLoop.current().start()
    if __name__ == '__main__':
        main()
发布了259 篇原创文章 · 获赞 6 · 访问量 3539

猜你喜欢

转载自blog.csdn.net/piduocheng0577/article/details/105059656