For anyone still having trouble with this create a folder in your user called ".ssh" and then create a file named "known_hosts" in that folder. Afterward, run the below code to create a host key for your site.
import pysftp
Hostname = "your_hostname"
Username = 'your_username'
Password = "your_password"
Port = 22
host = Hostname
# Loads .ssh/known_hosts
cnopts = pysftp.CnOpts()
hostkeys = None
if cnopts.hostkeys.lookup(host) == None:
print("New host - will accept any host key")
# Backup loaded .ssh/known_hosts file
hostkeys = cnopts.hostkeys
# And do not verify host key of the new host
cnopts.hostkeys = None
with pysftp.Connection(Hostname, username=Username, password=Password, port=Port, cnopts=cnopts) as sftp:
if hostkeys != None:
print("Connected to new host, caching its hostkey")
hostkeys.add(
host, sftp.remote_server_key.get_name(),
sftp.remote_server_key)
hostkeys.save(pysftp.helpers.known_hosts())
Finally, add the below code creating the connection object, the issue will be resolved and is better than disabling cynopts
cnopts = pysftp.CnOpts()
cnopts.hostkeys.load('C:/Users/_your_user_/.ssh/known_hosts')
I am not sure if this is the best way to go about it but this is the solution that I found after months of digging.