1 fromFtplibImport*2 ImportOs,readline3 ImportSYS4 classmyftp:5FTP = FTP ()#Create a link to an FTP object6 def __init__(Self, host, port=' +'):#Constructor Initialization7Self.ftp.connect (host=host,timeout=100)#connecting to an FTP server8 defLogin (self, User, passwd):#Login Function9Self.ftp.login (User=user, passwd=passwd)Ten Print(Self.ftp.welcome)#FTP display welcome message after successful login One A defDownLoadFile (self, Localfilepath, Remotefilepath): -File = Open (Localfilepath,'WB') -Self.ftp.retrbinary ("RETR%s"%( Remotefilepath), File.write)#download files to local the file.close () - returnTrue - - defDownloadfiletree (self, Localfiledir, Remotefiledir): + ifOs.path.isdir (localfiledir) = = False:#If the incoming local is not a directory -Os.makedirs (Localfiledir)#Create a new directory locally +SELF.FTP.CWD (Remotefiledir)#switch to remote directory ARemotefiles = Self.ftp.nlst ()#Pass all the files in the remote directory to the remotefiles variable. at forFileinchRemotefiles: -Local =os.path.join (localfilesdir, file) -Chang =Os.path.join (remotedir,file) - ifSelf . Isdir (Chang): - Self . Downloadfiletree (Local, Chang) - Else: in Self . DownLoadFile (Local, Chang) -SELF.FTP.CWD (".." ) to + defisdir (self, path): - ifOs.path.isdir (path) = =True: theSelf.juge =True * Else: $Self.juge =FalsePanax Notoginseng returnSelf.juge - the defUploadfiletree (self, Localfiledir, Remotefiledir): + ifOs.path.isdir (localfiledir) = =False: A Print('wrong! Please Input Dir') the ifOs.path.isdir (remotefiledir) = =False: + os.makedirs (Remotefiledir) -Localfiles =Os.listdir (Localfiledir) $ self.ftp.cwd (Remotefiledir) $ forLocalinchLocalfiles: -src =Os.path.join (Localfiledir, Local) -Chang =Os.path.join (remotefiledir,local) the ifOs.path.isdir (SRC): - Self . Uploadfiletree (Src,chang)Wuyi Else: the Self . UploadFile (Src,chang) -SELF.FTP.CWD (".." ) Wu - defUploadFile (self, Localfilepath, Remotefilepath): About ifOs.path.isfile (localfilepath) = =False: $ returnFalse -File = Open (LocalFile,"RB") -Self.ftp.storbinary ('STOR%s'%remotefile, file, 4096) - file.close () A + theFTP = MYFTP ('192.168.19.153')#instantiation of - $ defLogin (): theFtp.login ('Root','Root') the defUpdate (): the Print('\033[34muping.....\033[m') theFtp.uploadfiletree ('Main',"/xxx" ) - Print(' Done') in defDownLoad (): the Print('\033[34mdowning.....\033[m') theFtp.downloadfiletree ('Localdir','Remotedir') About Print(' Done') the defClose (): the self.ftp.quit () the + defMenu (): - Print("""\033[;32mwelcome \033[0m\n""") the Print("\ t (1) Login")Bayi Print("\ t (2) Update") the Print("\ t (3) DownLoad") the Print("\ t (4) Close") - whileTrue: -Choices = Raw_input ('\033[32mchoice>>\033[m'). Strip () the ifLen (choices) = = 0:Continue the ifChoices = ='1': Login () the elifChoices = ='2': Update () the elifChoices = ='3':D ownload () - elifChoices = ='4': Close () the theMenu ()
Http://www.cnblogs.com/zhuweiblog/p/5154752.html
Ftplib module to write a simple FTP service