title: Python - pysftp / paramiko - Verify host key using its fingerprint
body: <p>This code throws an exception. How can I verify an SSH fingerprint without storing it in a file? I believe the code below is designed for a public key. But the client with the SFTP server validated the fingerprint and did not get me the public key.</p>
  
  <pre><code>import os
  import shutil
  
  import pysftp
  import paramiko
  
  connection_info = {
      'server': "example.com",
      'user': "user",
      'passwd': "password",
      'target_dir': "out/prod",
      'hostkey': "ssh-rsa 2048 d8:4e:f1:f1:f1:f1:f1:f1:21:31:41:14:13:12:11:aa",
  }
  
  def move_files_from_server_to_local(server, localpath):
      target_dir = server['target_dir']
      keydata = "d8:4e:f1:f1:f1:f1:f1:f1:21:31:41:14:13:12:11:aa"
      key = paramiko.RSAKey(data=decodebytes(keydata))
      options = pysftp.CnOpts()
      options.hostkeys.add('example.com', 'ssh-rsa', key)
      with pysftp.Connection(
                      server['server'],
                      username=server['user'],
                      password=server['passwd'],
                      cnopts=options) as conn:
          conn.get_d(target_dir, localpath)
          delete_files_from_dir(conn, target_dir)
  
  move_files_from_server_to_local(connection_info, "/")
  </code></pre>
  
  <p>The code is based on <a href="https://stackoverflow.com/questions/38939454/verify-host-key-with-pysftp#43389508">Verify host key with pysftp</a>.</p>