首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >`cherrypy.InternalRedirect`和`cherrypy.HTTPRedirect`的区别?

`cherrypy.InternalRedirect`和`cherrypy.HTTPRedirect`的区别?
EN

Stack Overflow用户
提问于 2020-04-21 02:00:42
回答 1查看 334关注 0票数 0

我研究了Cherrypy web开发中的代码,

代码语言:javascript
复制
if returnpage != '':
   raise cherrypy.InternalRedirect(returnpage)
else:
   raise cherrypy.HTTPRedirect("/hqc")

在我做了一些调查之后,谷歌在这件事上没有多大帮助。我从cherrypy的__doc__中查看过,但是文档非常简洁。

代码语言:javascript
复制
>>>print(cherrypy.InternalRedirect.__doc__)
Exception raised to switch to the handler for a different URL.

    This exception will redirect processing to another path within the site
    (without informing the client). Provide the new path as an argument when
    raising the exception. Provide any params in the querystring for the new URL.

>>> print(cherrypy.HTTPRedirect.__doc__)
Exception raised when the request should be redirected.

    This exception will force a HTTP redirect to the URL or URL's you give it.
    The new URL must be passed as the first argument to the Exception,
    e.g., HTTPRedirect(newUrl). Multiple URLs are allowed in a list.
    If a URL is absolute, it will be used as-is. If it is relative, it is
    assumed to be relative to the current cherrypy.request.path_info.

    If one of the provided URL is a unicode object, it will be encoded
    using the default encoding or the one passed in parameter.

    There are multiple types of redirect, from which you can select via the
    ``status`` argument. If you do not provide a ``status`` arg, it defaults to
    303 (or 302 if responding with HTTP/1.0).

    Examples::

        raise cherrypy.HTTPRedirect("")
        raise cherrypy.HTTPRedirect("/abs/path", 307)
        raise cherrypy.HTTPRedirect(["path1", "path2?a=1&b=2"], 301)

    See :ref:`redirectingpost` for additional caveats.

我的问题是:-当您只需调用另一个处理程序时,为什么要费心重定向呢?-对于这两个重定向异常,有哪些实用的感应器?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-04-21 06:30:20

InternalRedirect只在服务器端进行处理,这意味着客户端将不知道重定向,因为就在客户端和服务器之间中介会话的HTTP协议而言,没有什么改变。在服务器端,我的意思是只有CherryPy才会意识到这种限制,如果您有一些中间服务器(比如nginx反向代理),它就不会看到任何不同的东西。

例如,如果客户端访问了一个url /page_one,然后使用了raise InternalRedirect('/page_two'),客户机(浏览器)将从/page_one url中的/page_two处理程序接收内容。如果引发常规HTTPRedirect,服务器将使用HTTP状态代码303 (或传递给异常的任何其他状态)和Location报头来结束第一个请求。然后是向/page_two发起另一个请求的客户端,基本上每个人都会知道重定向(有关HTTP重定向的更多信息)。在大多数情况下,这是更好的选择。

此外,还可以通过验证InternalRedirect属性来检测请求是否来自以前的cherrypy.request.prev。它将以前一个cherrypy.request对象作为其值或None

为了使用InternalRedirect (可能不是最好的例子),请签出这个产品/beta示例页面,此外,我还添加了一个工具,禁止客户机直接访问处理程序。

客户端将在同一个页面/中看到不同的内容。请注意,CherryPy生成的访问日志将记录最终处理请求的处理程序的url,在本例中,您将看到/_beta/_production

代码语言:javascript
复制
import random
import cherrypy


@cherrypy.tools.register('before_handler')
def private_handler():
    """End the request with HTTP 404 not found if the client
    tries to reach the handler directly instead of being
    internally redirected from other handler.
    """
    if cherrypy.request.prev is None:
        raise cherrypy.NotFound()


class MainApp:

    @cherrypy.expose
    def index(self):
        # 50/50 change of receiving production or the new SHINY beta page
        use_beta = random.randint(0, 1)
        if use_beta:
            raise cherrypy.InternalRedirect('/_beta')
        else:
            raise cherrypy.InternalRedirect('/_production')

    @cherrypy.tools.private_handler()
    @cherrypy.expose
    def _production(self):
        return (
            "<html>"
            "<h2>{}</h2>"
            "</html>"
        ).format(
            "Welcome to our awesome site!"
        )

    @cherrypy.tools.private_handler()
    @cherrypy.expose
    def _beta(self):
        return (
            "<html>"
            '<h1 style="color: blue">{}</h1>'
            "<p>{}</p>"
            "</html>"
        ).format(
            "Welcome to our awesome site!",
            "Here is our new beta content..."
        )


cherrypy.quickstart(MainApp())
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61334807

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档