from ftplib import FTP import os, time class FTPSync(): def __init__(self, FTPSRV, USER, PWD, FTPDIR, LOCALDIR, FILEEXTEN): self.FTPSRV = FTPSRV self.USER = USER self.PWD = PWD self.FTPDIR = FTPDIR self.LOCALDIR = LOCALDIR self.FILEEXTEN = FILEEXTEN def FileExists(self, ftp, file): filelist = [] #to store all files ftp.retrlines('LIST',filelist.append) # append to list for f in filelist: if file in f: return True return False def SizeLocal(self,file): return os.path.getsize(self.LOCALDIR + file) def SizeFTP(self,ftp, file): return ftp.size(file) def sync(self, timestr): #try: ftp = FTP(self.FTPSRV) ftp.login(self.USER, self.PWD) ftp.cwd(self.FTPDIR) ftp.sendcmd("TYPE i") for file in os.listdir(self.LOCALDIR): if file.endswith(self.FILEEXTEN) and file.startswith(time.strftime("%y")): print (timestr+"[FTP] Check: "+file) datei = open(self.LOCALDIR + file, "r") txt = sizeLocal1 = len(txt) sizeLocal2 = self.SizeLocal(file) sizeFTP = 0 datei.close() if (self.FileExists(ftp, file)): sizeFTP = self.SizeFTP(ftp, file) print(timestr+"[FTP] Size: "+str(sizeLocal1) + ","+str(sizeLocal2)+ ","+str(sizeFTP)) if (sizeLocal2 != sizeFTP and sizeLocal1 != sizeFTP): ftp.storbinary("STOR "+file, open(self.LOCALDIR + file, "rb")) print (timestr+"[FTP] Refresh: "+file) print timestr+("[FTP] End") #except: # print (timestr+"[FTP] Error") ftp.close()