我制作了一个脚本,将文件从本地机器复制到FTP服务器。我将此链接称为make Upload folders from local system to FTP using Python script,但现在我想使用Python脚本将文件从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()
答案 0 :(得分:2)
通常,如果FTP协议是您可以访问计算机的唯一方法,则无法将文件从一个远程FTP服务器传输到另一个远程FTP服务器。
FXP protocol允许这样做,但大多数FTP服务器通常不允许这样做。
如果您有其他服务器的其他访问权限,例如SSH,您当然可以自动登录服务器,然后在其上运行FTP以上传/下载到其他服务器或从其他服务器下载。