如何通过pysftp监控文件传输的进度

时间:2014-06-18 06:06:56

标签: python python-3.x ssh sftp pysftp

我正在使用python 3.3.2和pysftp创建一个备份实用程序,用于在我的网络上的另一台计算机上存储文件的副本。

我已经知道如何使用pysftp来传输这些文件但是我希望看到传输的进度(每秒字节数,完成百分比,剩余时间)而不是一些静态打印语句,表明传输已经开始或完成

这样的事情可能(文件名:3.4MiB / s | 40%< ######### =============> | 0:03:54 )

编辑: 我可能会建立一个进度条,如果我只知道如何获取pysftp的put命令来提供信息,那就是我想知道该怎么做

编辑: 我想在经过大量挖掘后,我在另一个问题上找到了答案:

How to see (log) file transfer progress using paramiko?

4 个答案:

答案 0 :(得分:4)

在get / put方法中都存在回调函数(参见 官方documentation

get(remotepath, localpath=None, callback=None, preserve_mtime=False)

回调可能如下所示:

lambda x,y: print("{} transfered/ {} to go".format(x,y))

答案 1 :(得分:1)

import math, pysftp

with pysftp.Connection(host, username, password) as sftp:
    sftp.get(remotepath, localpath, callback=lambda x,y: progressbar(x,y))

def progressbar(x, y):
    ''' progressbar for the pysftp
    '''
    bar_len = 60
    filled_len = math.ceil(bar_len * x / float(y))
    percents = math.ceil(100.0 * x / float(y))
    bar = '=' * filled_len + '-' * (bar_len - filled_len)
    filesize = f'{math.ceil(y/1024):,} KB' if y > 1024 else f'{y} byte'
    sys.stdout.write(f'[{bar}] {percents}% {filesize}\r')
    sys.stdout.flush()

[============================================================] 100% 4,342 KB

答案 2 :(得分:0)

您可以创建一个函数,该函数每隔10%就会打印一次转帐:

progressDict={}
progressEveryPercent=10

for i in range(0,101):
    if i%progressEveryPercent==0:
        progressDict[str(i)]=""

def printProgressDecimal(x,y):
    if int(100*(int(x)/int(y))) % progressEveryPercent ==0 and progressDict[str(int(100*(int(x)/int(y))))]=="":
        print("{}% ({} Transfered(B)/ {} Total File Size(B))".format(str("%.2f" %(100*(int(x)/int(y)))),x,y))
        progressDict[str(int(100*(int(x)/int(y))))]="1"

然后您可以像这样在get或put命令中调用该函数:

sftp.get(eachFile,localpath=localDirectory+eachFile, callback=lambda x,y: printProgressDecimal(x,y))

示例输出为:

0.00% (32768 Transfered(B)/ 1108907068 Total File Size(B))
10.00% (110919680 Transfered(B)/ 1108907068 Total File Size(B))
20.00% (221806592 Transfered(B)/ 1108907068 Total File Size(B))
30.00% (332693504 Transfered(B)/ 1108907068 Total File Size(B))
40.00% (443580416 Transfered(B)/ 1108907068 Total File Size(B))
50.00% (554467328 Transfered(B)/ 1108907068 Total File Size(B))
60.00% (665354240 Transfered(B)/ 1108907068 Total File Size(B))
70.00% (776241152 Transfered(B)/ 1108907068 Total File Size(B))
80.00% (887128064 Transfered(B)/ 1108907068 Total File Size(B))
90.00% (998047744 Transfered(B)/ 1108907068 Total File Size(B))
100.00% (1108907068 Transfered(B)/ 1108907068 Total File Size(B))

答案 3 :(得分:-1)

您可以编写自己的进度条,如果您对终端有所了解,这很简单。或者您可以使用progressbar。以下是文档中的一个简单示例:

@example
def example1():
    widgets = ['Test: ', Percentage(), ' ', Bar(marker=RotatingMarker()),
               ' ', ETA(), ' ', FileTransferSpeed()]
    pbar = ProgressBar(widgets=widgets, maxval=10000000).start()
    for i in range(1000000):
        # do something
        pbar.update(10*i+1)
    pbar.finish()

在这两种情况下,您都需要文件传输方法才能在传输过程中产生一些效果。如果你可以让它产生它收到的字节,那么无论如何都可以很容易地创建一个进度条。

相关问题