title: Prevent SFTP/SSH session timeout with paramiko
body: <p>I'm using <strong>paramiko</strong> to connect to an SFTP server on which I have to download and process some files.</p>
  
  <p>The server has a timeout set to 5 minutes, but some days it happens that the processing of the files can take longer than the timeout. So, when I want to change the working directory on the server to process some other files <code>sftp.chdir(target_dir))</code> I get an exception that the connection has timed out:</p>
  
  <blockquote>
    <p>File
    buildbdist.win32eggparamikosftp://ftp.py,
    line 138, in _write_all    raise
    EOFError()</p>
  </blockquote>
  
  <p>To counter this I thought that activating the keep alive would be the best option so I used the "set_keepalive" on the transport to set it to 30 seconds:</p>
  
  <pre><code>ssh = paramiko.SSHClient()
  ssh.set_missing_hostkey_policy(paramiko.AutoAddPolicy())
  ssh.connect(ssh_server, port=ssh_port, username=ssh_user, password=password)
  transport = ssh.get_transport()
  transport.set_keepalive(30)
  sftp = transport.open_sftp_client()
  </code></pre>
  
  <p>But nothing changes at all. The change has absolutely no effect. I don't know if I'm misunderstanding the concept of <code>set_keepalive</code> here or maybe the server (on which I have no access) ignores the <em>keep alive</em> packets.</p>
  
  <p>Isn't this the right way to counter this problem or should I try a different approach? I don't like the idea of "manually" sending some <code>ls</code> command to the server to keep the session alive.</p>