| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30-31 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 The |
#coding: Utf-8 from ctypes import * Import OS import sys import ftplib class myftp:ftp = Ftplib. FTP () Bisdir = False Path = "" Def __init__ (self, host, port= '): Self.ftp.set_debuglevel (2) #打开调试级别2, displays details #self. ftp.s ET_PASV (0) #0主动模式 1 #被动模式 self.ftp.connect (host, Port) def Login (self, User, passwd): Self.ftp.login (user, passwd) pr int Self.ftp.welcome def DownLoadFile (self, LocalFile, remotefile): File_handler = open (LocalFile, ' WB ') self.ftp.retr Binary ("RETR%s"% ( RemoteFile), File_handler.write) File_handler.close () return True def uploadfile (self, LocalFile, remotefile): If OS.P Ath.isfile (localfile) = = False:return False file_handler = open (LocalFile, "RB") Self.ftp.storbinary (' STOR%s '%remot Efile, File_handler, 4096) File_handler.close () return True def uploadfiletree (self, Localdir, Remotedir): If os.path.i Sdir (localdir) = = False:return False localnames = Os.listdir (localdir) print Remotedir self.ftp.cwd (remotedir) for Local in LOCALNAMES:SRC = OS.path.join (Localdir, local) if Os.path.isdir (SRC): self. Uploadfiletree (SRC, local) else:self. UploadFile (SRC, local) self.ftp.cwd ("...") return def downloadfiletree (self, Localdir, Remotedir): If Os.path.isdir ( Localdir) = = False:os.makedirs (localdir) self.ftp.cwd (remotedir) remotenames = Self.ftp.nlst () for file in Remotenam es:local = Os.path.join (localdir, file) if Self.isdir (file): Self. Downloadfiletree (local, file) else:self. DownLoadFile (local, file) self.ftp.cwd ("...") return def show (self, list): result = List.lower (). Split ("") if self . path in and ' <dir> ' in Result:self.bIsDir = True def isdir (self, path): Self.bisdir = False Self.path = P Ath #this ues callback function, that'll change Bisdir value self.ftp.retrlines (' LIST ', self.show) return Self.bisdir def close (self): self.ftp.quit () FTP = myftp (' ******** ') ftp.login (' * * *, ' * * * *) #ftp. DownLoadFile (' * * *. TXT ', ' Othersruntime.log ') #ok #ftp. UploadFile (' Runtime.log ', ' otheRsruntime.log ') #ok #ftp. Downloadfiletree (' BCD ', ' othersabc ') #ok #ftp. Uploadfiletree (' AAA ', "others") Ftp.close () print "ok!" |