如何使用DJango在Web浏览器中显示rsync - 进度? [英] How to show the rsync --progress in web browser using DJango?

查看:576
本文介绍了如何使用DJango在Web浏览器中显示rsync - 进度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

解决方案

基本原则是在子流程中运行rsync,公开一个web API并通过javascript获取更新>



这是一个例子。

  import subprocess 
import
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, )

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

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

while True:
output = proc.stdout.readline()
如果'检查'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()
如果int(m [0] [0])== 0:
break

print('\rFinished')

但这只会显示我们在标准输出中的进度( stdout )。



然而,我们可以修改此代码以将JSON作为JSON输出返回,该输出可以通过 progress webservice / API



在客户端使用,然后我们将写入javascript(ajax)联系我们的进度webservice / API ,并使用该信息更新客户端,例如文本msg,图像的宽度,某些div的颜色等


I am writing a Python/Django application which transfer files from server to the local machine using rsync protocol. We will be dealing with the large files so the progress bar is mandatory. --progress argument in rsync command does this beautifully. All the detail progresses are shown in the terminal. How can I show that progress in web browser? Is there any hook function or something like that? Or Can I store the progress in a log file, call it and update it every one minute or so?

解决方案

The basic principle is to run rsync in subprocess, expose a web API and get updates via javascript

Here's an example.

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')

But this only shows us the progress in our standard output (stdout).

We can however, modify this code to return the progress as a JSON output and this output can be made available via a progress webservice/API that we create.

On the client side use, we will then write javascript (ajax) to contact our progress webservice/API from time-to-time, and using that info update something client side e.g. a text msg, width of an image, color of some div etc

这篇关于如何使用DJango在Web浏览器中显示rsync - 进度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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