【问题标题】:How to copy file from FTP server to another FTP server (platform independently)如何将文件从 FTP 服务器复制到另一个 FTP 服务器(独立于平台)
【发布时间】:2017-12-11 11:33:06
【问题描述】:

我制作了一个脚本,将文件从本地机器复制到 FTP 服务器。我引用了这个链接来制作脚本Upload folders from local system to FTP using Python script,但现在我想使用 Python 脚本将文件从 FTP 复制到不同位置的另一台远程 FTP 机器。如何做到这一点?

可以使用rsync 命令完成文件复制,但我想使用 Python 脚本来完成。

代码:

import ftplib
import os

server = 'host'
username = 'name'
password = 'pass'
ftp = ftplib.FTP(server, username, password)
Path = 'path'#source
val = "/des/"#destination

def copy(source,destination):
     print(source)
     print(destination)
     os.chdir(source)
     ftp.cwd(destination)
     if "C:\\" in Path or "c:\\" in Path:
        ftp_path = (source).split("\\")[-1]
     else:
        ftp_path = (source).split("/")[-1]
     directory = destination+ftp_path
     mkdir(directory)
     ftp.cwd(directory)
     read = os.listdir(source)
     for file in read:
         print(file) 
         if "C:\\" in Path or "c:\\" in Path:
            Type = source + "\\" + file
         else:
            Type = source + "/" + file
         print(Type)
         print()
         if os.path.isdir(Type):#If Type is Folder then it will create new 
folder
             copy(Type,directory+"/")
         elif os.path.isfile(Type):#If Type is file then it will create file
             print(Type) 
             current_dir = ftp.pwd() + "/"
             f = Type
             fh = open(f, 'rb')
             ftp.storbinary('STOR %s' % current_dir + file, fh)
             fh.close()

def mkdir(path):
    #print(path)
    ftp.mkd(path)
copy(Path,val)



ftp.close()

【问题讨论】:

  • 什么意思“远程机器”

标签: python ftp ftplib


【解决方案1】:

一般来说,如果 FTP 协议是您访问机器的唯一方式,您不能将文件从一个远程 FTP 服务器传输到另一个远程 FTP 服务器。

FXP protocol 允许这样做,但大多数 FTP 服务器通常不允许这样做。

如果您对其中一台服务器有其他访问权限,例如 SSH,您当然可以自动登录到该服务器,然后在其上运行 FTP 客户端,以向另一台服务器上传/下载。

【讨论】:

    猜你喜欢
    • 2018-07-08
    • 1970-01-01
    • 2015-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多