简体   繁体   English

PYSFTP脚本上传和下载

[英]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 . Stack Overflow上的pysftp文档和其他答案似乎也表明可以通过设置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 . 根据pysftp文档,您需要在~/.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). 您可以通过ssh命令连接到服务器来执行此操作(它将询问您是否要向主机文件添加新密钥;如果您信任远程主机的指纹,则只需键入yes )。

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. 官方文档还指出了如何完全禁用主机检查,但不建议这样做,因为它会破坏SSH身份识别的关键安全功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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