您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

如何使用DJango在Web浏览器中显示rsync --progress?

如何使用DJango在Web浏览器中显示rsync --progress?

基本原理是

这是一个例子。

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我们创建的a使该输出可用。

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

Go 2022/1/1 18:38:13 有350人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶