Search code examples
pythonparamikopysftp

PYSFTP script to Upload and Download


I need some help I'm getting below message when i try to connect. The script Im executing is below. Any suggestions as to how I can fix this issue?

Also, once I get this issue fixed Im looking to add to this script the ability to listen until the result file is available and to download it from a different remote directory.

Thank you, in advance

No hostkey for host mysftpserver.com found.
Exception AttributeError: "'Connection' object has no attribute 
'_sftp_live'" in <bound method Connection.__del__ of <pysftp.Connection 
object at 0x101d8cd50>> ignored

Process finished with exit code 0

Script

import pysftp as sftp

def sftpExample():

    try:
        s = sftp.Connection(host='mysftpserver', port=1211, username='myusername', password='mypassword')

        remotepath='/Orders/incoming/myfile.csv'
        localpath='/Users/myusername/projects/order88.csv'
        s.put(localpath,remotepath)

        s.close()

    except Exception, e:
        print str(e)

sftpExample()

Solution

  • The pysftp documentation and other answers on Stack Overflow also seem to suggest that the error can be resolved by setting cnopts.hostkeys = None.

    So the full code would be something like:

    import pysftp
    def sftpExample():
        cnopts = pysftp.CnOpts()
        cnopts.hostkeys = None  
        with pysftp.Connection('hostname', username='me', password='secret', cnopts=cnopts) as sftp:
            sftp.put('/Users/myusername/projects/order88.csv','/Orders/incoming/myfile.csv')  # upload file to public/ on remote
            sftp.close()
    
    sftpExample()
    

    Hope it helps!