title: Use Paramiko AutoAddPolicy with pysftp
body: <p>This code is not working:</p>
  
  <pre><code>def sftp_connection(self):
      import pysftp
      connection = pysftp.Connection(self.host, username=self.system_name,
                    private_key=os.path.join(HOME, '.ssh', 'id_rsa'))
  
      # in the next lines I try to use AutoAddPolicy
      client = connection.sftp_client()
      client.load_host_keys(os.path.expanduser('~/.ssh/known_hosts'))
      client.set_missing_host_key_policy(paramiko.client.AutoAddPolicy)
      return connection
  </code></pre>
  
  <p>This is the exception:</p>
  
  <pre class="lang-none prettyprint-override"><code>Traceback (most recent call last):
    File "/home/u/src/myapp-glo/myapp_doxis_archiv/tests/test_doxis_archiv.py", line 85, in test_beleg_to_archiv__ftpservercontext
      info_dict = beleg_to_archiv(beleg, self.archiv_belegart)
    File "/home/u/src/myapp-glo/myapp_doxis_archiv/beleg_to_archiv.py", line 28, in beleg_to_archiv
      transfer_log=send_data_via_ftp(temp_directory, archiv_belegart.doxis_archiv)
    File "/home/u/src/myapp-glo/myapp_doxis_archiv/beleg_to_archiv.py", line 71, in send_data_via_ftp
      with doxis_archiv.sftp_connection() as sftp:
    File "/home/u/src/myapp-glo/myapp_doxis_archiv/models.py", line 43, in sftp_connection
      private_key=os.path.join(HOME, '.ssh', 'id_rsa'))
    File "/home/u/local/lib/python2.7/site-packages/pysftp/__init__.py", line 132, in __init__
      self._tconnect['hostkey'] = self._cnopts.get_hostkey(host)
    File "/home/u/local/lib/python2.7/site-packages/pysftp/__init__.py", line 71, in get_hostkey
      raise SSHException("No hostkey for host %s found." % host)
  SSHException: No hostkey for host localhost found.
  </code></pre>
  
  <p>I get the exception before I try to set the <code>host_key_policy</code>.</p>
  
  <p>I could not find a different way to access the client instance via pysftp.</p>
  
  <p>Is there a way to set <a href="http://docs.paramiko.org/en/2.4/api/client.html#paramiko.client.AutoAddPolicy" rel="noreferrer"><code>AutoAddPolicy</code></a> before I get the exception?</p>
  
  <p>There is a related <a href="https://stackoverflow.com/questions/38939454/verify-host-key-with-pysftp">question</a>. My question is about how to apply one of the several solutions which are provided in the old question.</p>