2017-12-11 15 views
1

로컬 컴퓨터에서 FTP 서버로 파일을 복사하는 스크립트를 만들었습니다. 이 링크를 사용하여 스크립트 Upload folders from local system to FTP using Python script을 만들었지 만, 지금은 파이썬 스크립트를 사용하여 FTP에서 다른 위치의 다른 원격 컴퓨터로 파일을 복사하려고합니다. 이 작업을 수행하는 방법?FTP 서버에서 다른 FTP 서버로 파일을 복사하는 방법 (플랫폼 독립적)

파일 복사는 rsync 명령을 사용하여 수행 할 수 있지만 파이썬 스크립트를 사용하여이 작업을 수행하려고합니다.

코드 : FTP 프로토콜을 사용하면 컴퓨터에 액세스 할 수있는 유일한 방법 인 경우 일반적으로

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

질문은 이미 답이 여기있다 : https://stackoverflow.com/questions/11573817/how-to-download-a-file-via-ftp-with-python-ftplib#11573946이 trivilal에 대한 검색 더 질문. 추신. 이 스크립트에는 다른 GUI가 없습니다. –

+0

내 GUI에있는 파일을 복사하기 위해 스크립트를 사용했습니다. 잠시만 기다려주십시오. 내 코드를 보여 드리겠습니다. –

+0

* "원격 시스템"*의 의미는 무엇입니까? –

답변

2

, 당신은, 다른 원격 FTP 서버에 하나 개의 원격 FTP 서버에서 파일을 전송할 수 없습니다.

허용되는 것은 FXP protocol이지만 일반적으로 대부분의 FTP 서버에서는 허용되지 않습니다.

SSH와 같은 서버 중 하나에 다른 액세스 권한이있는 경우 물론 서버에 자동으로 로그인 한 다음 FTP를 실행하여 다른 서버와 업로드/다운로드 할 수 있습니다.