【问题标题】:How to transfer zip files from one server to another using Python SFTP/Paramiko如何使用 Python SFTP/Paramiko 将 zip 文件从一台服务器传输到另一台服务器
【发布时间】:2021-12-25 06:08:13
【问题描述】:

我正在尝试使用 Python 脚本在两台服务器之间进行 SFTP。起初,我尝试将文件下载到本地计算机,但由于权限错误而失败,不知道为什么限制将文件复制到本地文件夹。

任何想法都会受到赞赏。下面是代码sn-p(只完成了一半)

import paramiko

host= <defined here>
user = <defined here>
pswd = <defined here>

ssh = paramiko.SSHClient()
# automatically add keys without requiring human intervention
ssh.set_missing_host_key_policy( paramiko.AutoAddPolicy() )
ssh.connect(host, username=user, password=pswd)
ftp = ssh.open_sftp()

ftp.get(source_path,destination_path)

ftp.close()

【问题讨论】:

    标签: python sftp paramiko pysftp


    【解决方案1】:

    编辑:
    请亲自阅读传说,@Martin Prikryl 的评论以获得更深入的解释和更好的解决方案。

    这能解决问题吗?

    import paramiko
    
    from_host = <defined here>
    from_user = <defined here>
    from_pass = <defined here>
    
    to_host = <defined here>
    to_user = <defined here>
    to_pass = <defined here>
    
    from_client = paramiko.SSHClient()
    # automatically add keys without requiring human intervention
    from_client.set_missing_host_key_policy( paramiko.AutoAddPolicy() )
    from_client.connect(from_host, username=from_user, password=from_password)
    
    to_client = paramiko.SSHClient()
    # automatically add keys without requiring human intervention
    to_client.set_missing_host_key_policy( paramiko.AutoAddPolicy() )
    to_client.connect(to_host, username=to_user, password=to_password)
    
    # Open an sftp client for both servers
    from_ftp = from_client.open_sftp()
    to_ftp = to_client.open_sftp()
    
    # Open the file on the from server. Returns a file like object
    with from_ftp.open(source_path) as f:
        # putfo takes an open file object and creates it at the specifies path
        to_ftp.putfo(f, path_where_you_want_the_file_on_to_server)  # need filename
    
    from_ftp.close()
    to_ftp.close()
    

    SFTPClient documentation

    【讨论】:

    最近更新 更多