Python版本为:Python3.5

1 from ftplib import FTP,error_perm # 加载ftp模块 2 import os 3 4 class FTPSync(object): 5 conn = FTP() 6 7 def __init__(self,host,port=21): 8 self.conn.connect(host,port) 9 10 def login(self,username,password): 11 self.conn.login(username,password) 12 self.conn.encoding = "GB2312" 13 self.conn.set_debuglevel(2) #打开调试级别2,显示详细信息 14 self.conn.set_pasv(True) 15 #0主动模式 1 #被动模式 16 print(self.conn.welcome) 17 18 def _ftp_list(self,line): 19 li = line.split(' ') 20 if self.ftp_dir_name == li[-1] and "<DIR>" in li: 21 self._is_dir = True 22 23 def _is_ftp_file(self, ftp_path): 24 try: 25 if ftp_path in self.conn.nlst(os.path.dirname(ftp_path)): 26 return True 27 else: 28 return False 29 except error_perm as e: 30 return False 31 32 def _is_ftp_dir(self,ftp_path): 33 """ 34 用来判断所给的路径是文件还是文件夹 35 """ 36 ftp_path = ftp_path.rstrip('/') 37 ftp_parent_path = os.path.dirname(ftp_path) 38 self.ftp_dir_name = os.path.basename(ftp_path) 39 self._is_dir = False 40 if ftp_path == '.' or ftp_path == './' or ftp_path == '': 41 self._is_dir = True 42 else: 43 try: 44 self.conn.retrlines('LIST %s' %ftp_parent_path,self._ftp_list) 45 except error_perm as e: 46 return self._is_dir 47 return self._is_dir 48 49 50 def get_file(self,ftp_path="",local_path="."): 51 print(ftp_path) 52 ftp_path = ftp_path.rstrip('/') 53 54 file_name = os.path.basename(ftp_path) 55 56 # 如果本地路径是目录,下载文件到该目录 57 if os.path.isdir(local_path): 58 file_handler = open(os.path.join(local_path,file_name),'wb') 59 self.conn.retrbinary("RETR %s"%(ftp_path),file_handler.write) 60 file_handler.close() 61 62 # 如果本地路径不是目录,但上层目录存在,则按照本地路径的文件名作为下载的文件名称 63 elif os.path.isdir(os.path.dirname(local_path)): 64 file_handler = open(local_path,'wb') 65 self.conn.retrbinary("RETR %s"%(ftp_path),file_handler.write) 66 file_handler.close() 67 # 如果本地路径不是目录,且上层目录不存在,则退出 68 else: 69 print('EROOR:The dir:%s is not exist' %os.path.dirname(local_path)) 70 71 72 def get_dir(self,ftp_path,local_path=".",begin=True): 73 74 if not self._is_ftp_dir(ftp_path): 75 self.get_file(ftp_path=ftp_path, local_path=local_path) 76 return 77 78 if begin: 79 local_path = os.path.join(local_path, os.path.basename(ftp_path)) 80 81 #如果本地目录不存在,则创建目录 82 if not os.path.isdir(local_path): 83 os.mkdir(local_path) 84 85 #进入ftp目录,开始递归查询 86 self.conn.cwd(ftp_path) 87 88 ftp_files = self.conn.nlst() 89 90 for file in ftp_files: 91 local_file = os.path.join(local_path, file) 92 #如果file ftp路径是目录则递归上传目录(不需要再进行初始化begin的标志修改为False) 93 #如果file ftp路径是文件则直接上传文件 94 if self._is_ftp_dir(file): 95 self.get_dir(file, local_file, False) 96 elif "idea" in file: 97 pass 98 else: 99 self.get_file(ftp_path=file, local_path=local_file) 100 101 #如果当前ftp目录文件已经遍历完毕返回上一层目录 102 self.conn.cwd("..") 103 104 105 def get_all_dir(self): 106 ftp_files = self.conn.nlst() 107 for file in ftp_files: 108 self.get_dir(file,"D:\\ftp",True) 109 110 111 def put_file(self,local_path,ftp_path="."): 112 ftp_path = ftp_path.rstrip('/') 113 if os.path.isfile(local_path): 114 file_handler = open(local_path,'rb') 115 local_file_name = os.path.basename(local_path) 116 117 #如果远程路径是个目录,则上传文件到这个目录,文件名不变 118 if self._is_ftp_dir(ftp_path): 119 self.conn.storbinary('STOR %s'%os.path.join(ftp_path,local_file_name), file_handler) 120 121 #如果远程路径的上层是个目录,则上传文件,文件名按照给定命名 122 elif self._is_ftp_dir(os.path.dirname(ftp_path)): 123 print('STOP %s'%ftp_path) 124 self.conn.storbinary('STOR %s'%ftp_path, file_handler) 125 #如果远程路径不是目录,且上一层的目录也不存在,则提示给定远程路径错误 126 else: 127 print('STOR %s'%ftp_path, file_handler) 128 129 def put_dir(self,local_path,ftp_path=".",begin=True): 130 ftp_path = ftp_path.rstrip('/') 131 132 if not os.path.isdir(local_path): 133 print('ERROR:The dir:%s is not exist' %local_path) 134 return 135 136 #当本地目录存在时上传 137 #上传初始化:如果给定的ftp路径不存在需要创建,同时将本地的目录存放在给定的ftp目录下。 138 # 本地目录下文件存放的路径为ftp_path = ftp_path + os.path.basename(local_path) 139 #例如,将本地的文件夹a上传到ftp的a/b目录下,则本地a目录下的文件将上传的ftp的a/b/a目录下 140 if begin: 141 if not self._is_ftp_dir(ftp_path): 142 self.conn.mkd(ftp_path) 143 ftp_path = os.path.join(ftp_path,os.path.basename(local_path)) 144 145 # 如果上传路径是文件夹,则创建目录 146 if not self._is_ftp_dir(ftp_path): 147 self.conn.mkd(ftp_path) 148 149 150 #进入本地目录,开始递归查询 151 os.chdir(local_path) 152 local_files = os.listdir('.') 153 for file in local_files: 154 ftp_file = os.path.join(ftp_path,file) 155 #如果file本地路径是目录则递归上传文件(不需要再进行初始化begin的标志修改为False) 156 #如果file本地路径是文件则直接上传文件 157 if os.path.isdir(file): 158 self.put_dir(file,ftp_file,False) 159 elif "idea" in file: 160 pass 161 else: 162 self.put_file(file,ftp_file) 163 164 #如果当前本地目录文件已经遍历完毕返回上一层目录 165 os.chdir('..') 166 167 168 if __name__ == "__main__": 169 ftp = FTPSync('FTPIP') 170 ftp.login('username',"password") 171 # ftp.get_file("ftppath") 172 # ftp.get_dir("ftppath","localpath",True) 173 # ftp.get_dir("","",True) 174 # ftp.get_all_dir("") 175 # ftp.put_file('localpath') 176 ftp.put_dir('localpath',begin=True) 177 178 179 #文件名命名的时候不要带有空格,否则文件会传输不上去