我在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
仅在服务器端进行处理,这意味着客户端将不知道该重定向,因为就中介客户端和服务器之间的会话的HTTP协议而言,什么都没有改变。在服务器端,我的意思是,只有CherryPy会受到补偿,[[aware,如果您有一些中间服务器(例如nginx反向代理),则不会有任何不同。
/page_one
,然后您使用了raise InternalRedirect('/page_two')
,则客户端(浏览器)将在/page_two
URL中从/page_one
处理程序接收内容。如果引发常规HTTPRedirect
,服务器将以HTTP状态码303
(或传递给异常的任何其他状态)和Location
的/page_two
标头结束第一个请求。然后是将向/page_two
发起另一个请求的客户端,基本上每个人都会[[aware重定向(more info about HTTP redirection)。在大多数情况下,这是更好的选择。另外,您可以通过验证InternalRedirect
属性来检测请求是否来自先前的cherrypy.request.prev
。它将先前的cherrypy.request
对象作为其值或None
。
InternalRedirect
,请检查此生产/测试版示例页面,此外,我添加了一个工具来禁止客户端直接与处理程序联系。客户端将在同一页面/
中看到不同的内容。请注意,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())