Python FTP代码示例

时间:2016-03-11 08:49:27

标签: python ftp

我需要将相同的本地文件上传到FTP服务器上的不同目标文件名。不是Python的专家做这样的功能。使用Python 2.7和任何好的代码示例表示赞赏。感谢。

提前谢谢, 林

1 个答案:

答案 0 :(得分:1)

以下内容可以帮助您入门:

from ftplib import FTP    

hosts = [('10.1.0.1', 'abc', '123'), ('10.1.0.2', 'abc', '123')]
local_file = r'/my_folder/my_file.txt'
remote_file = 'my_file.txt'

for host, name, password in hosts:
    f = FTP(host, name, password)
    f.cwd('my_remote_folder')

    with open(local_file, 'rb') as f_local:
        f.storbinary('STOR {}'.format(remote_file), f_local)

    print "{} - done".format(host)
    f.quit()

这会将my_file.txt从单个源位置上传到hosts列表中的每个主机。它将文件上传到每台服务器上的相同位置。

相关问题