Timeout in paramiko (python)

Kukosk picture Kukosk · Mar 18, 2012 · Viewed 44.9k times · Source

I'm looking for a way to set a timeout for this:

transport = paramiko.Transport((host, port))
transport.connect(username = username, password = password)
sftp = paramiko.SFTPClient.from_transport(transport)
sftp.get(remotepath, localpath)
sftp.close()
transport.close()

Answer

Kukosk picture Kukosk · Mar 18, 2012

The connection timeout can be set with the timeout parameter (that indicated the number of seconds for the time out as described here) of the connect function.

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(host, username=username, password=password, timeout=10)
sftp = ssh.open_sftp()
sftp.get(remotepath, localpath)
sftp.close()