35

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

1 Answer 1

71

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()
Sign up to request clarification or add additional context in comments.

6 Comments

@kukosk It would help if you mentioned the unit in which timeout is to be given, I guess its seconds.
What's the default timeout, I don't think I found it mentioned in the docs either, although they do mention timeout (float) – an optional timeout (in seconds) for the TCP connect
Here's some more background, if timeout is not specified the client falls into blocking mode: docs.python.org/3/library/socket.html#socket-timeouts
Lots of default timeout definitions pop up at github.com/jbouse-debian/paramiko/blob/master/paramiko/…, all given in seconds
Note that timeout parameter sets TCP timeout. There are also banner_timeout and auth_timeout which you may want to adjust as well.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.