Python SCPClient复制进度ch

2024-04-27 13:15:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我是SCPClient模块的新成员

我有样品复印件

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

这个代码运行良好。在

但是,由于我复制了几个大文件,复制进度需要时间,一味地等到它完成是不好的用户体验。在

有没有什么可以让我监视它复制了多少?以及复制成功与否的结果?在

SCPClient有官方文件要看吗?在


Tags: 模块文件sourcegetputaswith样品
2条回答

你看了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()

正如老鹰所说,他们很好地打印出进展情况。但是,打印频率太高,会消耗大量的资源。在

要控制打印频率,我们需要重写“发送文件”或“发送文件”功能

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

相关问题 更多 >