Python SCPClient复制进度检查

时间:2018-04-04 18:15:25

标签: python python-2.x

我是SCPClient模块的新手

我有复制样本

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

此代码效果很好。

但是,由于我复制了几个大文件,复制进度需要时间,盲目等待,直到完成用户体验不好。

无论如何,我可以监控它复制了多少?如果复制成功与否,请抓住结果?

SCPClient是否有官方文件可供阅读?

2 个答案:

答案 0 :(得分:0)

您是否看过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()

答案 1 :(得分:0)

正如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()