Paramiko的sftp.get超时

问题描述 投票:1回答:1

我发现这是有用的Setting timelimit for sftp.get() of Paramiko module但这里的回调函数也考虑了连接建立的时间。我只需要限制SFTP get文件传输时间。我试图修改回调函数,如下所示,但它不起作用。这是我的代码。

class TimeLimitExceeded(Exception):
    pass

def _timer(start_time, timelimit=5):
    elapsed_time = time.time()-start_time
    if elapsed_time > timelimit:
        raise TimeLimitExceeded

if __name__=="__main__":

    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh.connect(ip, port, username, password)
    sftp = ssh.open_sftp()
    try:
        start_time=time.time()
        sftp.get(remote_path, local_path, _timer(start_time))
    except TimeLimitExceeded:
        print ("The operation took too much time to complete")
    finally:
        sftp.close()
        ssh.close()
python python-3.x sftp paramiko
1个回答
0
投票
sftp.get(remote_path, local_path, _timer(start_time))

你没有在这里传递_timer作为回调,你正在调用_timer函数并传递它的返回值(它没有)。

这是正确的(正如原始代码所做的那样):

sftp.get(remote_path, local_path, _timer)
© www.soinside.com 2019 - 2024. All rights reserved.