#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import pycurl


# Accept new hosts and add them to HOSTS_FILE
def accept_new_hosts(known_key, found_key, match):
    print((known_key, found_key, match))
    return pycurl.KHSTAT_FINE_ADD_TO_FILE


# Configure & download
crl = pycurl.Curl()
crl.setopt(pycurl.SSH_KNOWNHOSTS, "/home/mdubois/.ssh/known_hosts")
crl.setopt(pycurl.SSH_KEYFUNCTION, accept_new_hosts)

crl.setopt(pycurl.URL, "sftp://test.rebex.net/readme.txt")
crl.setopt(pycurl.USERPWD, "demo:password")

fp = open("/tmp/readme.txt", "wb")
crl.setopt(pycurl.WRITEFUNCTION, fp.write)

crl.perform()

# Re-read hosts file & print length
with open("/tmp/known_hosts") as f:
    print(len(f.readlines()))

