Copy Code code as follows:
#-*-Encoding:utf8-*-
Import OS
Import Sys
Import Ftplib
Class Ftpsync (object):
def __init__ (self):
Self.conn = Ftplib. FTP (' 10.22.33.46 ', ' user ', ' pass ')
SELF.CONN.CWD ('/') # Remote FTP directory
Os.chdir ('/data/') # Local download directory
def get_dirs_files (self):
U ' get current directory and file, put into Dir_res list '
Dir_res = []
Self.conn.dir ('. ', Dir_res.append)
Files = [F.split (None, 8) [-1] for F in Dir_res if F.startswith ('-')]
dirs = [F.split (None, 8) [-1] for F in Dir_res if F.startswith (' d ')]
return (Files, dirs)
Def walk (Self, Next_dir):
print ' Walking to ', Next_dir
SELF.CONN.CWD (Next_dir)
Try
Os.mkdir (Next_dir)
Except OSError:
Pass
Os.chdir (Next_dir)
Ftp_curr_dir = Self.conn.pwd ()
Local_curr_dir = OS.GETCWD ()
files, dirs = Self.get_dirs_files ()
print "Files:", files
Print "dirs:", dirs
For f in Files:
Print Next_dir, ': ', F
Outf = open (f, ' WB ')
Try
Self.conn.retrbinary (' RETR%s '% F, outf.write)
Finally
Outf.close ()
For D in dirs:
Os.chdir (Local_curr_dir)
SELF.CONN.CWD (Ftp_curr_dir)
Self.walk (d)
def run (self):
Self.walk ('. ')
def main ():
f = Ftpsync ()
F.run ()
if __name__ = = ' __main__ ':
Main ()