用于在Realtime中在浏览器上显示输出的Python Bottle Web应用程序

时间:2016-02-07 13:52:32

标签: python bottle

我正在尝试使用pySmartDL构建基于瓶子的下载管理器。 但是,我无法在下载过程中将进度条输出到浏览器。

app.py

from bottle import route, run, debug, template, request, static_file, error
import os
from pySmartDL import SmartDL

@route('/static/:filename#.*#')
def send_static(filename):
    return static_file(filename, root='./static/')

@route('/',method='GET')
def index():
    return template('index.tpl')

@route('/download/', method='POST')
def result():
    if request.POST.get('url','').strip():
        url = request.POST.get('url', '').strip()
        #url = "http://mirror.ufs.ac.za/7zip/9.20/7za920.zip"
        dest = "C:\\Downloads\\" # or '~/Downloads/' on linux
        obj = SmartDL(url, dest)
        obj.start(blocking=None)
        # [*] 0.23 Mb / 0.37 Mb @ 88.00Kb/s [##########--------] [60%, 2s left]
        path = obj.get_dest()
        out = template('out',out=obj.get_progress_bar(length=20),path=path)
        return out
@error(500)
def mistake500(code):
    return '<h3>Error!</h3>'
debug(True)
run(host='localhost', port=8080)

下载文件后,#############将立即打印在浏览器中。

out.tpl

% include('header.tpl', title='VTU Results Hub')
<table  class="pure-table">
{{out}}
{{path}}
</table>
% include('footer.tpl')

有没有办法让我可以在浏览器的实时显示进度条。

2 个答案:

答案 0 :(得分:0)

您必须启动后台线程,在后台下载文件...

在您的HTML放置中

(将每5秒刷新一次)

<meta http-equiv="refresh" content="5;    URL=http://www.yourdomain.com/status/">

在.py文件中创建状态处理程序并发布更新...

@route('/status/', method='GET')
def result():
    #get the bar and return your template again...

答案 1 :(得分:0)

您可以使用gevents的异步事件处理在服务器端执行此操作。实际上,bottlepy文档在http://bottlepy.org/docs/dev/async.html处有一个示例:

from gevent import monkey; monkey.patch_all()

from time import sleep
from bottle import route, run

@route('/stream')
def stream():
    yield 'START'
    sleep(3)
    yield 'MIDDLE'
    sleep(5)
    yield 'END'

run(host='0.0.0.0', port=8080, server='gevent')

同时查看先前询问的&amp;在这里回答了问题:Streaming Connection Using Python Bottle, Multiprocessing, and gevent

相关问题