使用CherryPy执行自动重定向 [英] Doing auto redirect using CherryPy

查看:61
本文介绍了使用CherryPy执行自动重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚发现CherryPy.我正在阅读本教程,到目前为止一切顺利.在执行此操作时,我想创建一个"BUSY DOING WORK"初始屏幕,从本质上讲,我有一个python函数,例如,用10000条记录更新一个sqlite表.我想做的是让CherryPy在更新数据库时显示 busy.html 页面,当数据库操作完成时,我想将用户重定向回 main.html 页面.到目前为止,我只遇到过

I've just discovered CherryPy. I am going through the tutorial, so far so good. While doing it I wanted to create a "BUSY DOING WORK" splash screen, essentially I have a python function that for example updates an sqlite table with 10000 records. What I want to do is get CherryPy to display a busy.html page while the database is being updated, when the database operation completes I want to redirect the user back to the main.html page. So far I have only come across

dev update_db(self):
      #Code to update the database goes here
      return "busy.html"<----show user that work is being done
      #database has completed
      CherryPy.redirect "main.html"

但是 return 只是退出该函数.无论如何,当数据库正在更新时,是否会向用户显示一个临时的初始屏幕,一旦完成,就将用户返回到另一个页面.我想一种替代方法是在现有页面的顶部显示一条消息,但是我不知道CherryPy是否具有类似于Flask的消息功能.

But return simply exits the function. Is there anyway of doing presenting the user with a temporary splashscreen, while the database is being updated then returning the user back to another page once its complete. I suppose an alternative is to have a message flash across the top of the existing page, But I don't know if CherryPy has a flash message feature much like Flask.

推荐答案

恕我直言,您可以使用生成器来实现,这是最新(v3.8)cherrypy

IMHO, you can achieve this with generators and here is a link from latest (v3.8) cherrypy documentation. However, you should take into account the following issue in the docs

流生成器很性感,但是它们会对HTTP造成破坏.CherryPy允许您针对特定情况流输出:需要花费几分钟才能生成的页面,或需要部分内容的页面会立即输出到客户端.由于上面概述的问题,通常最好扁平化(缓冲)内容而不是流内容.仅当流式传输的好处胜过风险时,否则请执行其他操作.

Streaming generators are sexy, but they play havoc with HTTP. CherryPy allows you to stream output for specific situations: pages which take many minutes to produce, or pages which need a portion of their content immediately output to the client. Because of the issues outlined above, it is usually better to flatten (buffer) content rather than stream content. Do otherwise only when the benefits of streaming outweigh the risks.

发电机在文档中有一定的局限性

Generators have some limitations as written in the documentation

如果该处理程序方法是流生成器,则无法手动修改页面处理程序中的状态或标头,因为在将标头写入客户端之前,不会迭代该方法.这包括引发异常,例如HTTPError,NotFound,InternalRedirect和HTTPRedirect .要在修改标头时使用流式生成器,您将必须返回一个与页面处理程序分离(或嵌入到页面处理程序中)的生成器.

you cannot manually modify the status or headers within your page handler if that handler method is a streaming generator, because the method will not be iterated over until after the headers have been written to the client. This includes raising exceptions like HTTPError, NotFound, InternalRedirect and HTTPRedirect. To use a streaming generator while modifying headers, you would have to return a generator that is separate from (or embedded in) your page handler.

由于流式传输时已将标头写入客户端,因此引发重定向异常无法帮助您在长期运行任务后将其重定向到其他页面.如果我是你,我会屈服这个

Because the headers have already been written to the client when streaming, raising redirection exception cannot help to redirect to different page after your long running task. If I were you, I would yield this

<meta http-equiv="refresh" content="0;URL='/anotherpage'" /> 

或以最终的收成

<script>window.location.href="/anotherpage";</script>

我为您提供了示例代码.我希望这能给您一个主意.

I coded and example for you. I hope this gives you an idea.

# encoding: utf-8
import time
import cherrypy

class Root(object):

    @cherrypy.expose
    def index(self):
        content = '''<!DOCTYPE html>
            <html>
            <head>
            <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
            <script>
            $(document).ready(function(){
                $("body").append("<p>jQuery Ready</p>");
                setTimeout(function(){
                    $("body").html("Redirecting Please Wait...")
                    }, 2500);
            });
            </script>
            </head>
            <body>
            <p>Page Content1</p>
            <p>Page Content2</p>
            <p>Page Content3</p>
            <p>Page Content4</p>
            <p>Page Content5</p>
            <p>Page Content Final</p>
            </body>
            </html>
            '''
        for line in content.split("\n"):
            yield line
            time.sleep(1)
        else:
            yield '''<meta http-equiv="refresh" content="5;URL='/anotherpage'" />'''

    index._cp_config = {'response.stream': True}

    @cherrypy.expose
    def anotherpage(self):
        return "Another Page"


cherrypy.quickstart(Root(), "/")

这篇关于使用CherryPy执行自动重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆