[paramiko] How to use settimeout with from_transport
Todd Whiteman
toddw at activestate.com
Thu Mar 15 19:18:22 PDT 2007
James Bardin wrote:
> I've wondered how to do this myself, and I have a method that works, but
> maybe someone will let us know if there's a better way.
>
> My program uses a large number of connections simultaneously, so to set
> a short timeout for everything I do the following:
>
> from socket import setdefaulttimeout
> setdefaulttimeout(2)
>
Ouch, thats global settings for socket timeout.
I used the following, which seems to work on a per socket basis, though
it still seems a little hacky to me:
# After I establish the SSH transport connection
sftp = paramiko.SFTP.from_transport(connection)
# sftp.sock in this case is actually a paramiko.Channel object
sftp.sock.settimeout(self._socket_timeout)
Robey can probably offer a better sol'n :)
Cheers,
Todd
> This setting propagates up and is used by the sockets created by paramiko.
>
>
> If it makes a difference, I'm building my connection with:
> client = SSHClient()
> client.connect(...)
> sftp = client.open_sftp()
>
> -jim
>
>
>
> Manpreet Singh Khurana wrote:
>> Hi
>> I have a simple utility which opens SSH connection to remote server by using
>>
>> t = paramiko.Transport((hostname, port))
>> and then doing connect by
>>
>> t.connect(username=username, password=password, hostkey=hostkey)
>>
>> The i am using SFTPClient to open an sftp session across an open ssh
>> Transport and do remote file operations.
>> sftp = paramiko.SFTPClient.from_transport(t)
>>
>> Now with this SFTP Client object i am opening a file on the remote server
>> rPath= sftp.file(fileName, 'rb')
>> and reading the file from this path
>> data = rPath.read(32768)
>>
>> Now I want to use the settimeout API to set the timeout value of this
>> socket so that id the read operation takes time more than the
>> configured value we should be
>> able to timeout that socket
>>
>> How can this be done
>> Can i use sftp.settimeout(60) directly
>> Do we have a sample code to understand the usage
>> Can somebody tell me how this is done.
>> This is urgent, please help.
>>
>> MSK
More information about the paramiko
mailing list