【问题标题】:How to show the rsync --progress in web browser using DJango?如何使用 DJango 在网络浏览器中显示 rsync --progress?
【发布时间】:2012-11-11 12:20:56
【问题描述】:

我正在编写一个 Python/Django 应用程序,它使用 rsync 协议将文件从服务器传输到本地机器。我们将处理大文件,因此进度条是强制性的。 rsync 命令中的 --progress 参数很好地做到了这一点。所有详细进度都显示在终端中。如何在网络浏览器中显示该进度?是否有任何钩子功能或类似的东西?或者我可以将进度存储在日志文件中,每隔一分钟左右调用一次并更新一次吗?

【问题讨论】:

  • 浏览器是通过 rsync 还是通过 Web 浏览器与服务器通信?两个同时?

标签: python django rsync


【解决方案1】:

基本原理是在子进程中运行rsync,暴露一个web API并通过javascript获取更新

这是一个例子。

import subprocess
import re
import sys

print('Dry run:')
cmd = 'rsync -az --stats --dry-run ' + sys.argv[1] + ' ' + sys.argv[2]
proc = subprocess.Popen(cmd,
                        shell=True,
                        stdin=subprocess.PIPE,
                        stdout=subprocess.PIPE,)

remainder = proc.communicate()[0]
mn = re.findall(r'Number of files: (\d+)', remainder)
total_files = int(mn[0])
print('Number of files: ' + str(total_files))

print('Real rsync:')
cmd = 'rsync -avz  --progress ' + sys.argv[1] + ' ' + sys.argv[2]
proc = subprocess.Popen(cmd,
                        shell=True,
                        stdin=subprocess.PIPE,
                        stdout=subprocess.PIPE,)

while True:
             output = proc.stdout.readline()
if 'to-check' in output:
             m = re.findall(r'to-check=(\d+)/(\d+)', output)
             progress = (100 * (int(m[0][1]) - int(m[0][0]))) / total_files
             sys.stdout.write('\rDone: ' + str(progress) + '%')
             sys.stdout.flush()
             if int(m[0][0]) == 0:
                      break

print('\rFinished')

但这只是向我们展示了标准输出 (stdout) 的进度。

但是,我们可以修改此代码以将进度作为 JSON 输出返回,并且此输出可以通过我们创建的 progress webservice/API 提供。

在客户端使用时,我们将编写 javascript (ajax) 以不时联系我们的progress webservice/API,并使用该信息更新客户端的某些内容,例如文本消息、图像宽度、某些 div 的颜色等

【讨论】:

猜你喜欢
  • 2011-06-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-13
  • 2020-05-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多