简体   繁体   中英

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()

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!

Per the pysftp documentation you need to have the remote host key in your ~/.ssh/known_hosts . You can do this by connecting to the server via the ssh command (it will ask you if you want to add a new key to your hosts file; simply type yes if you trust the remote host's fingerprint).

The official documentation also points out how to disable host checking entirely, but this is NOT recommended as it would defeat critical security features for SSH identification.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM