0

I'm working on creating a script to upload multiple files to a server through an application. The user will be able to select multiple files, all of which will be uploaded to the server via a Python program. I had a look for SFTP python libraries that can use to connect to the server and came across pysftp. I downloaded the latest version and tried to connect using the following script:

import tkinter.filedialog
import pysftp as sftp

def upload():
    try:
        s = sftp.Connection(host='host',username='username',password='password',port=portnumber)
        localpath='C:/Python/Code/example.txt'
        remotepath='/home/example'
        s.put(localpath,remotepath)

        s.close

    except Exception as e:
        print(str(e))
upload()

I ran this, only to find an error that I had no hostkey. I did some research, found out you could turn off the need for hostkey verification by adding:

cnopts = pysftp.CnOpts()
cnopts.hostkeys = None

and then changing the original line to:

s = sftp.Connection(host='hostip',username='username',password='password',port=portno, cnopts=cnopts) 

So that it wouldn't require a host key. This still was having the same error (pysftp__init__.py:61: UserWarning: Failed to load HostKeys). I tried changing this around for about an hour before getting frustrated and trying a work around I had previously avoided. I downgraded to a previous version of pysftp (pysftp==0.2.8) which didn't require hostkey verification. I've ran the following code again:

import tkinter.filedialog
import pysftp as sftp


def upload():
    try:
        s = sftp.Connection(host='hostip',username='username',password='password',port=portno)
        localpath='C:/Python/Code/example.txt'
        remotepath='/home/test'
        s.put(localpath,remotepath)

        s.close

except Exception as e:
    print(str(e))
upload()

Only to get the following error: 'Failure'

I've been trying this for hours. As you might've guessed, I'm pretty new to Python and programming in general so I might have made a beginner error somewhere. For note, I can connect to my server using SFTP through Filezilla and using SSH through puTTY (I just use the host, port, username and password) - so there isn't a problem with that.If anyone could help me solve this (either by using the latest version of pysftp, the older version or a completely different library) I would appreciate it massively.

4
  • 1
    First, you have to verify the host key. Do not try to avoid that! See stackoverflow.com/a/43389508/850848 Oct 19, 2017 at 21:17
  • @MartinPrikryl Appreciate the reply - I've had a look at that link but can't figure out where to find my key to put in the known_hosts file? I've reupdated to the latest version of pysftp because I need the security for this script when I've completed it.
    – Chuck
    Oct 20, 2017 at 10:13
  • There's a link in my answer: Where do I get SSH host key fingerprint to authorize the server? It's for my WinSCP SFTP client, but most information there is valid in general. Oct 20, 2017 at 10:30
  • Had a read before but struggled to do this, I've ended up using paramiko as suggested by the below answer. Thanks for the help though! Hopefully I'll learn more about how to verify the host key in the future.
    – Chuck
    Oct 22, 2017 at 16:35

1 Answer 1

2

pysftp is just a wrapper around paramiko to abstract and simplify sftp operations, if you are having issues with pysftp you can use parmiko directly, since paramiko is a dependency of pysftp you will already have it installed.

from paramiko import SSHClient

client = SSHClient()
client.set_missing_host_key_policy(paramiko.client.AutoAddPolicy)
client.connect('hostname', user='user', password='password')
sftp = client.open_sftp()
sftp.put(localpath, remotepath)
1
  • 2
    Used this in the end, thanks very much! My code just in case anyone has the same problem in the future: import paramiko \n ssh_client = paramiko.SSHClient() ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh_client.connect(hostname='host',port=portno,username=username,password=password) s = ssh_client.open_sftp() s.put(localpath,remotepath)
    – Chuck
    Oct 22, 2017 at 16:39

Not the answer you're looking for? Browse other questions tagged or ask your own question.