python--FTP两个文件夹间的同步

  1. # -*- coding: utf-8 -*-  
  2. ''''''' 
  3.     ftp自动检测源文件夹的更新,将源文件夹更新的内容拷贝到目标文件夹中 
  4.     使用树的层序遍历算法,支持深度目录拷贝 
  5. '''  
  6. import os  
  7. from ftplib import FTP  
  8. import os,sys,string,datetime,time  
  9. import shutil  
  10. import socket  
  11.   
  12. class MyUpdateMonitor(object):  
  13.     def __init__(self, hostaddr, username, password, remotedir_old, remotedir_new, port = 21):  
  14.         self.hostaddr = hostaddr  
  15.         self.username = username  
  16.         self.password = password  
  17.         self.remotedir_old = remotedir_old  
  18.         self.remotedir_new = remotedir_new  
  19.         # self.port = port  
  20.         self.ftp = FTP()  
  21.         # 源文件文件队列  
  22.         self.FolderListOld = []  
  23.         # 目标文件文件队列  
  24.         self.FolderListNew = []  
  25.   
  26.     def __del__(self):  
  27.         self.ftp.close()  
  28.         self.FolderListOld.clear()  
  29.         self.FolderListNew.clear()  
  30.   
  31.     def FtpLogin(self):  
  32.         ftp = self.ftp  
  33.         try:  
  34.             timeout = 300  
  35.             socket.setdefaulttimeout(timeout)  
  36.             ftp.set_pasv(True)  
  37.             print u'开始连接到 %s' %(hostaddr)  
  38.             ftp.connect(hostaddr)  
  39.             print u'成功连接到 %s' %(hostaddr)  
  40.             print u'开始登录到 %s' %(hostaddr)  
  41.             ftp.login(username, password)  
  42.             print u'成功登录到 %s' %(hostaddr)  
  43.             ftp.getwelcome()  
  44.         except Exception, e:  
  45.             print 'find exception now:',e  
  46.   
  47.     # 使用树的层序遍历来检查文件目录  
  48.     def LevelOrderFolder(self):  
  49.         # 新增文件起始位置和终止位置  
  50.         start = 0  
  51.         end = 0  
  52.         try:  
  53.             # 将根目录放入队列中  
  54.             self.FolderListOld.append(self.remotedir_old)  
  55.             self.FolderListNew.append(self.remotedir_new)  
  56.             while not (0 == len(self.FolderListOld)):  
  57.                 end = start  
  58.                 # 将文件夹下的文件全部压入队列  
  59.                 if os.path.isdir(self.FolderListOld[0]):  
  60.                     files = os.listdir(self.FolderListOld[0])  
  61.   
  62.                     for file in files:  
  63.                         self.FolderListOld.append(os.path.join(self.FolderListOld[0], file))  
  64.                     # 确定新增文件在队列中的位置  
  65.                     end += len(files)  
  66.                 # 将已经查看的文件夹删除  
  67.                 del self.FolderListOld[0]  
  68.                 # 检查目标文件夹该级目录  
  69.                 if os.path.isdir(self.FolderListNew[0]):  
  70.                     # 将该级目录的文件都列出  
  71.                     files = os.listdir(self.FolderListNew[0])  
  72.                     # 检查源文件该级目录下的文件在目标该级目录下是否有  
  73.                     for file in self.FolderListOld[start:end]:  
  74.                         temp = file.split('\\')  
  75.                         if temp[-1in files:  
  76.                             # 这里判断文件大小是否一致,不一致拷过去  
  77.                             if os.path.isfile(file) and not  os.path.getsize(file) == os.path.getsize(os.path.join(self.FolderListNew[0], temp[-1])):  
  78.                                 print 'Find the file(%s) size is changed!\n' % file  
  79.                                 # print r'Start delete...\n'  
  80.                                 # os.remove(os.path.join(self.FolderListNew[0], temp[-1]))  
  81.                                 # print r'delete is over...\n'  
  82.   
  83.                                 print 'Start Copy...\n'  
  84.                                 shutil.copyfile(file, os.path.join(self.FolderListNew[0], temp[-1]))  
  85.                                 print 'Copy is over...\n'  
  86.                             # # 如果是文件夹存在,但是修改过,没有必要全部拷贝文件夹,可以到文件夹中拷贝单个文件  
  87.                             # if os.path.isfile(file) and not (os.path.getmtime(file) == os.path.getmtime(os.path.join(self.FolderListNew[0], temp[-1]))):  
  88.                             #     print 'Find the file(%s) size is changed!\n' % file  
  89.                             #     changetime = os.path.getmtime(file) #以毫秒为单位的时间,自1970年开始到现今  
  90.                             #     changetime = float(changetime)  
  91.                             #     print 'Change Time is', time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime(changetime)), r'\n'  
  92.                             #  
  93.                             #     print 'Start Copy...\n'  
  94.                             #     shutil.copyfile(file, os.path.join(self.FolderListNew[0], temp[-1]))  
  95.                             #     print 'Copy is over...\n'  
  96.                         else:  
  97.                             if os.path.isdir(file):  
  98.                                 # 如果是文件夹不存在使用,目录树拷贝  
  99.                                 print 'Find the folder(%s) is updated!\n' % file  
  100.                                 print 'Start Copy...\n'  
  101.                                 shutil.copytree(file, os.path.join(self.FolderListNew[0], temp[-1]))  
  102.                                 print 'Copy is over...\n'  
  103.                             else:  
  104.                                 # 如果是文件  
  105.                                 print 'Find the file(%s) is updated!\n' % file  
  106.                                 print 'Start Copy...\n'  
  107.                                 shutil.copyfile(file, os.path.join(self.FolderListNew[0], temp[-1]))  
  108.                                 print 'Copy is over...\n'  
  109.                         self.FolderListNew.append(os.path.join(self.FolderListNew[0], temp[-1]))  
  110.   
  111.                 del self.FolderListNew[0]  
  112.   
  113.                 start = end - 1  
  114.         except Exception, e:  
  115.             print 'find exception now:',e  
  116.   
  117.   
  118. if __name__ == '__main__':  
  119.   
  120.     # 配置如下变量  
  121.     hostaddr = r'10.204.16.28' # ftp地址  
  122.     username = r' ' # 用户名  
  123.     password = r' ' # 密码  
  124.   
  125.     remotedir_old = r'\\10.204.16.28\Home\TDME\Test\Old\TMUMH_1.6.1055'  
  126.     remotedir_new = r'\\10.204.16.28\Home\TDME\Test\New\TMUMH_1.6.1055'  
  127.   
  128.     monitorfileupdae = MyUpdateMonitor(hostaddr, username, password, remotedir_old, remotedir_new)  
  129.     monitorfileupdae.FtpLogin()  
  130.   
  131.     while True:  
  132.         print 'Start Check Update...\n'  
  133.         monitorfileupdae.LevelOrderFolder()  
  134.         print 'Check Update is Over...\tSleep one hour...'  
  135.         time.sleep(3600)  
  136.   
  137.     print 'hello'  

猜你喜欢

转载自blog.csdn.net/hanyuyang19940104/article/details/80431876