.Tang

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

3种输出方法:write render redirectimport tornado.ioloop

import tornado.web
import tornado.httpserver  # 非阻塞
import tornado.options   # 提供了多种选择 python xxx.py --port=xxxx
from tornado.options import define,options
define('port',default=8000,help='run port',type=int)  # windows通过Ctrl+鼠标左键
define('version',default=0.1,help='version',type=str)


class MainHandler(tornado.web.RequestHandler):
    def get(self):
        self.render("ces.html")  # 写入页面


class RecHandler(tornado.web.RequestHandler):
    def get(self):
        self.redirect(r"/rec1")  # 在进入这个网址(路由),自动跳转到rec1去


class Rec1Handler(tornado.web.RequestHandler):
    def get(self):
        self.write("由/rec跳转过来")

        # request
        self.write(self.request.remote_ip)  # remote_ip属性查询发送请求的ip地址
                            # (浏览器->virtualBox->虚拟环境 所以得到的不是浏览器的ip)
        print(self.request.full_url())  # full_url方法查询完整的url
        print(self.request.request_time())  # 处理请求的时间
application = tornado.web.Application(
    handlers=[
    (r"/",MainHandler),
    (r'/rec', RecHandler),
    (r'/rec1', Rec1Handler),
     ],
    template_path='templates',  # 表明页面html的路径
    debug=True           # 上传代码后服务器自动重启
)

if __name__ == '__main__':
    tornado.options.parse_command_line()   # 通过sys.arg获取命令行输入参数(python xxx.py --port=xxx)
    print(options.port)
    print(options.version)
    http_server = tornado.httpserver.HTTPServer(application)  # 非阻塞
    application.listen(options.port)
    tornado.ioloop.IOLoop.instance().start()   # 启动io循环

# 知识点

# 输出3种:

    # write 输出到浏览器。 先写入缓冲(flush直接写到浏览器)再一起写入浏览器
        write 可写入 三种类型 unicode字符串,dict,和 /b # render写入页面 html文件 # template_path = 'templates', # 这句写在路由表里,表明路径templates # redirect 自动跳转页面 # 在进入这个网址(路由),自动跳转到另一个网址 # request #.remote_ip查询发送请求的ip地址 # debug调式 虫子--晶体管

 

posted on 2018-02-22 16:14  .Tang  阅读(230)  评论(0编辑  收藏  举报