重定向

作者: 小吉头 | 来源:发表于2020-08-09 15:35 被阅读0次

一、使用redirect()

import tornado.web
import tornado.ioloop


class IndexHandler(tornado.web.RequestHandler):
    def get(self,*args,**kwargs):
        self.redirect("http://www.baidu.com")

app = tornado.web.Application([
    (r'/index/$',IndexHandler),
])

app.listen(8888)

tornado.ioloop.IOLoop.instance().start()

浏览器响应头信息如下,浏览器会访问Location地址,示例返回的状态码是302

Response Headers:
HTTP/1.1 302 Found
Content-Length: 0
Date: Sat, 08 Aug 2020 13:45:35 GMT
Location: http://www.baidu.com
Content-Type: text/html; charset=UTF-8
Server: TornadoServer/6.0.4

查看redirect()的源码:

    def redirect(self, url: str, permanent: bool = False, status: int = None) -> None:
        """Sends a redirect to the given (optionally relative) URL.

        If the ``status`` argument is specified, that value is used as the
        HTTP status code; otherwise either 301 (permanent) or 302
        (temporary) is chosen based on the ``permanent`` argument.
        The default is 302 (temporary).
        """
        if self._headers_written:
            raise Exception("Cannot redirect after headers have been written")
        if status is None:
            status = 301 if permanent else 302
        else:
            assert isinstance(status, int) and 300 <= status <= 399
        self.set_status(status)
        self.set_header("Location", utf8(url))
        self.finish()

二、自己实现重定向

根据上面redirect()的源码可以自己定义状态码和Location,返回状态码302

import tornado.web
import tornado.ioloop


class IndexHandler(tornado.web.RequestHandler):
    def get(self,*args,**kwargs):
        self.set_status(302)
        self.set_header("Location","http://www.baidu.com")

app = tornado.web.Application([
    (r'/index/$',IndexHandler),
])

app.listen(8888)

tornado.ioloop.IOLoop.instance().start()

三、使用RedirectHandler类

import tornado.web
import tornado.ioloop
from tornado.web import RedirectHandler

app = tornado.web.Application([
    (r'/redirect/$',RedirectHandler,{"url":"http://www.baidu.com"}),
])

app.listen(8888)

tornado.ioloop.IOLoop.instance().start()

查看RedirectHandler类源码,最终还是调用了redirect(),返回的状态码是301

class RedirectHandler(RequestHandler):

    def initialize(self, url: str, permanent: bool = True) -> None:
        self._url = url
        self._permanent = permanent

    def get(self, *args: Any) -> None:
        to_url = self._url.format(*args)
        if self.request.query_arguments:
            # TODO: figure out typing for the next line.
            to_url = httputil.url_concat(
                to_url,
                list(httputil.qs_to_qsl(self.request.query_arguments)),  # type: ignore
            )
        self.redirect(to_url, permanent=self._permanent)

状态码301和302的区别

301表示永久重定向,比如访问 http://www.baidu.com 会跳转到 https://www.baidu.com301跳转在浏览器上有缓存,如果更换跳转地址,需清除浏览器的缓存
302表示临时重定向,比如未登陆的用户访问个人信息会重定向到登录页面

相关文章

  • 第07章重定向管道

    输出重定向案例 > < 脚本中使用重定向 2.输入重定向及结合案例 管道 | 重定向和管道的符号对比。重定向输出到...

  • shell 笔记 Day1

    重定向: (覆盖重定向), >>(追加重定向) , 2>(重定向错误信息) , &>(错误正确都重定...

  • 《Linux就该这么学 》笔记(六)| 管道符、重定向和环境变量

    1. 重定向 重定向技术的 5 种模式 标准覆盖输出重定向 标准追加输出重定向 错误覆盖输出重定向 错误追加输出重...

  • 永久性重定向和302临时性重定向

    什么是重定向? 所谓重定向就是将网页自动转向重定向,即:301永久性重定向和302临时性重定向。实施301后,新网...

  • 2019-06-13 重定向301和302

    什么是重定向? 所谓重定向就是将网页自动转向重定向,即:301永久性重定向和302临时性重定向。实施301后,新网...

  • 重定向

    输出重定向 输入重定向 错误重定向 管道 shell中特殊符号

  • Linux重定向day13

    1.重定向概述2.重定向的输出输入3.进程管道技术 一、重定向概述 什么是重定向:Linux重定向是指修改原来默认...

  • Linux高级

    一.重定向命令 学习目标 能够使用重定向命令将终端显示内容重定向到文件 1. 重定向命令的介绍 重定向也称为输出重...

  • vue-router -其他

    一、 重定向 重定向也是通过 routes 配置来完成,下面例子是从 /me重定向到/home`: 重定向的目标也...

  • uos 输入输出与重定向

    1、实验-输出重定向 2、实验-错误重定向 3、实验-双重输出重定向 4、实验-输入重定向 5、实验-管道 6、实...

网友评论

      本文标题:重定向

      本文链接:https://www.haomeiwen.com/subject/hykkdktx.html