Python SCPClient复制进度检查

Python SCPClient copy progress check

我是 SCPClient 模块的新手

我有副本样本

 with SCPClient(ssh.get_transport()) as scp:
    scp.put(source, destination)

此代码运行良好。

但是,由于我是复制几个大文件,复制进度比较慢,一味的等到完成是不好的用户体验。

有没有办法让我监控它复制了多少?复制成功与否获取结果?

SCPClient有官方文档可以看吗?

您看过 Github page? 他们提供了如何执行此操作的示例:

from paramiko import SSHClient
from scp import SCPClient
import sys

ssh = SSHClient()
ssh.load_system_host_keys()
ssh.connect('example.com')

# Define progress callback that prints the current percentage completed for the file
def progress(filename, size, sent):
    sys.stdout.write("%s\'s progress: %.2f%%   \r" % (filename, float(sent)/float(size)*100) )

# SCPCLient takes a paramiko transport and progress callback as its arguments.
scp = SCPClient(ssh.get_transport(), progress = progress)

scp.put('test.txt', '~/test.txt')
# Should now be printing the current progress of your put function.

scp.close()

正如Eagle 回答的那样,他们很好地打印出了进度。但是打印频率太高,会消耗很多资源。

要控制打印频率,我们需要覆盖 _send_file 或 _send_files 函数

def _send_files(self, files):
...
        buff_size = self.buff_size
        chan = self.channel
        # Add time control
        time_cursor=datetime.datetime.now()
        while file_pos < size:
            chan.sendall(file_hdl.read(buff_size))
            file_pos = file_hdl.tell()
            now=datetime.datetime.now()
            # Status check every one sec
            if self._progress and (now-time_cursor).seconds>1:
                self._progress(basename, size, file_pos)
                time_cursor=now
        chan.sendall('\x00')
        file_hdl.close()
        self._recv_confirm()