`cherrypy.InternalRedirect` 与 `cherrypy.HTTPRedirect` 之间的区别?
Difference between `cherrypy.InternalRedirect` vs. `cherrypy.HTTPRedirect`?
我在Cherrypy web开发中学习了如下代码,
if returnpage != '':
raise cherrypy.InternalRedirect(returnpage)
else:
raise cherrypy.HTTPRedirect("/hqc")
在我做了一些研究后,Google 在这种情况下没有多大帮助。
我已经从 cherrypy 的 __doc__
查过,但那里的文档非常简洁。
>>>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.
我的问题是:
- 当您可以简单地调用另一个处理程序时,为什么还要费心重定向?
- 两种重定向异常分别有哪些实用场景?
InternalRedirect
仅在服务器端处理,这意味着客户端不会知道该重定向,因为根据在客户端之间调解 session 的 HTTP 协议和服务器,没有任何改变。在服务器端,我的意思是只有 CherryPy 会 知道 的修订,如果你有一些中间服务器(比如 nginx 反向代理),它不会看到任何不同。
例如,如果客户端访问了 url /page_one
,然后您使用了 raise InternalRedirect('/page_two')
,则客户端(浏览器)将从 /page_two
处理程序中接收内容/page_one
url。如果您提出常规 HTTPRedirect
,服务器将以 303
的 HTTP 状态代码(或您传递给异常的任何其他状态)和 Location
[=45] 结束第一个请求=] 到 /page_two
。然后是客户端将向 /page_two
发起另一个请求,基本上每个人都会 意识到 重定向 (more info about HTTP redirection)。大多数时候这是更好的选择。
此外,您可以通过验证 cherrypy.request.prev
属性 来检测请求是否来自之前的 InternalRedirect
。它将以前的 cherrypy.request
object 作为其值或 None
.
为了可能(也许不是最好的例子)使用 InternalRedirect
,请查看此 production/beta 示例页面,此外,我添加了一个工具来禁止客户端访问直接处理程序。
客户端会在同一个页面看到不同的内容/
。请注意,CherryPy 生成的访问日志将记录最终处理请求的处理程序的 url,在这种情况下,您将看到 /_beta
或 /_production
.
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())
我在Cherrypy web开发中学习了如下代码,
if returnpage != '':
raise cherrypy.InternalRedirect(returnpage)
else:
raise cherrypy.HTTPRedirect("/hqc")
在我做了一些研究后,Google 在这种情况下没有多大帮助。
我已经从 cherrypy 的 __doc__
查过,但那里的文档非常简洁。
>>>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.
我的问题是: - 当您可以简单地调用另一个处理程序时,为什么还要费心重定向? - 两种重定向异常分别有哪些实用场景?
InternalRedirect
仅在服务器端处理,这意味着客户端不会知道该重定向,因为根据在客户端之间调解 session 的 HTTP 协议和服务器,没有任何改变。在服务器端,我的意思是只有 CherryPy 会 知道 的修订,如果你有一些中间服务器(比如 nginx 反向代理),它不会看到任何不同。
例如,如果客户端访问了 url /page_one
,然后您使用了 raise InternalRedirect('/page_two')
,则客户端(浏览器)将从 /page_two
处理程序中接收内容/page_one
url。如果您提出常规 HTTPRedirect
,服务器将以 303
的 HTTP 状态代码(或您传递给异常的任何其他状态)和 Location
[=45] 结束第一个请求=] 到 /page_two
。然后是客户端将向 /page_two
发起另一个请求,基本上每个人都会 意识到 重定向 (more info about HTTP redirection)。大多数时候这是更好的选择。
此外,您可以通过验证 cherrypy.request.prev
属性 来检测请求是否来自之前的 InternalRedirect
。它将以前的 cherrypy.request
object 作为其值或 None
.
为了可能(也许不是最好的例子)使用 InternalRedirect
,请查看此 production/beta 示例页面,此外,我添加了一个工具来禁止客户端访问直接处理程序。
客户端会在同一个页面看到不同的内容/
。请注意,CherryPy 生成的访问日志将记录最终处理请求的处理程序的 url,在这种情况下,您将看到 /_beta
或 /_production
.
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())