在使用FTP模塊時候,首先需要定義FTP的實例,才可以對FTP進行操作
from ftplib import FTP import timeimport os sys_time = time.time() sys_time_array = time.localtime(sys_time) current_time = time.strftime("%Y-%m-%d %H:%M:%S:",sys_time_array) #FTP登錄 def ftp_login(host,port,user,password): ftp = FTP() #定義一個空的FTP實例 try: ftp.connect(host,port) #指定ip、端口,連接FTP except: print "FTP connect failed!!!" try: ftp.login(user,password) #指定賬號、密碼,登錄FTP except: print "FTP login failed!!!" else: return ftp #返回ftp實例
擁有一個FTP實例之后,我們就可以對其進行操作。
#FTP目錄全量下載 def ftp_download(ftp,remote_path,local_path): try: file_list = ftp.nlst(remote_path) #列出ftp指定目錄下的所有文件,這里返回一個列表。 except: print "remote_path error!!!" else: key = os.path.exists(local_path) #檢測本地存放文件的目錄是否存在,若不存在,則創建。 if key == 'True': pass else: cmd = 'mkdir ' + local_path + ' -p' os.system(cmd) print "downloading!!!" try: for file in file_list: #將列表中的文件遍歷一遍 bufsize = 1024 file_name = file.split('/')[-1] #因為返回的文件是全路徑的,因此這里要處理一下,單獨獲取文件名 local_file = open(local_path+file_name,'wb') #打開本地的文件 ftp.retrbinary('RETR %s'%(file),local_file.write,bufsize) #用二進制的方式將FTP上的文件寫到本地文件里面 ftp.set_debuglevel(0) local_file.close() #記得要關閉文件 except: print "%s %s download failed!!!" %(current_time,remote_path) else: print "%s %s download succeed!!!" %(current_time,remote_path)
下面是筆者在工作中編寫的一段代碼,其功能是簡單地實現FTP的下載,上傳,文件校驗
#!/usr/bin/env python #-*- coding: utf-8 -*- from ftplib import FTP import sys,time,os,hashlib #定義時間 sys_time = time.time() sys_time_array = time.localtime(sys_time) current_time = time.strftime("%Y-%m-%d %H:%M:%S:",sys_time_array) #定義FTP實例 class ftp(object): def __init__(self,ip,port,user,password): self.ip = ip self.port = port self.user = user self.password = password #FTP登錄模塊 def ftp_login(self): ftp = FTP() try: ftp.connect(self.ip,self.port) except: print "FTP connect failed!!!" try: ftp.login(self.user,self.password) except: print "FTP login failed!!!" else: return ftp #FTP下載 def ftp_download(self,remote_path,local_path): ftp = FTP() try: ftp.connect(self.ip,self.port) except: print "FTP connect failed!!!" try: ftp.login(self.user,self.password) except: print "FTP login failed!!!" else: try: file_list = ftp.nlst(remote_path) except: print "remote_path error!!!" else: key = os.path.exists(local_path) if str(key) == 'True': pass else: os.makedirs(local_path) print "downloading!!!" try: for file in file_list: bufsize = 1024 file_name = file.split('/')[-1] local_file = open(local_path+file_name,'wb') ftp.retrbinary('RETR %s'%(file),local_file.write,bufsize) ftp.set_debuglevel(0) local_file.close() except: print "%s %s download failed!!!" %(current_time,remote_path) else: print "%s %s download successfully!!!" %(current_time,remote_path) #FTP下載 def ftp_upload(self,remote_path,local_path): ftp = FTP() try: ftp.connect(self.ip,self.port) except: print "FTP connect failed!!!" try: ftp.login(self.user,self.password) except: print "FTP login failed!!!" else: try: ftp.mkd(remote_path) except: pass try: file_list = os.walk(local_path) for root,dirs,files in file_list: for file in files: local_file = local_path + file remote_file = remote_path + file bufsize = 1024 fp = open(local_file,'rb') ftp.storbinary('STOR ' + remote_file, fp, bufsize) fp.close() except: print "%s %s upload failed!!!" %(current_time,local_path) else: print "%s %s upload successfully!!!" %(current_time,local_path) #FTP文件校驗 def check(self,remote_path,local_path): #將FTP臨時文件下載到本地臨時目錄 tmp_path = '/tmp' + remote_path ftp = FTP() try: ftp.connect(self.ip,self.port) except: print "FTP connect failed!!!" try: ftp.login(self.user,self.password) except: print "FTP login failed!!!" else: try: file_list = ftp.nlst(remote_path) except: print "remote_path error!!!" else: key = os.path.exists(tmp_path) if str(key) == 'True': pass else: os.makedirs(tmp_path) print "checking!!!" try: for file in file_list: bufsize = 1024 file_name = file.split('/')[-1] local_file = open(tmp_path+file_name,'wb') ftp.retrbinary('RETR %s'%(file),local_file.write,bufsize) ftp.set_debuglevel(0) local_file.close() except: print "cloudn't check!!! it may be cause by your wrong FTP info!!!" #校驗FTP臨時文件的md5值 tmp_file_list = os.walk(tmp_path) tmp_file_md5s = {} for root,dirs,files in tmp_file_list: for tmp_file in files: tmp_file_path = tmp_path+tmp_file f_tmp = open(tmp_file_path,'r') fcont = f_tmp.read() f_tmp.close() fmd5 = hashlib.md5(fcont) tmp_file_md5s[tmp_file] = fmd5.hexdigest() #校驗本地文件的md5值 local_file_list = os.walk(local_path) local_file_md5s = {} for root,dirs,files in local_file_list: for local_file in files: local_file_path = local_path+local_file f_local = open(local_file_path,'r') fcont = f_local.read() f_local.close() fmd5 = hashlib.md5(fcont) local_file_md5s[local_file] = fmd5.hexdigest() #開始校驗文件md5值是否相同 for k,v in tmp_file_md5s.items(): try: if tmp_file_md5s[k] == local_file_md5s[k]: pass else: ftp = FTP() ftp.connect(self.ip,self.port) ftp.login(self.user,self.password) bufsize = 1024 local_file = open(local_path+k,'wb') ftp.retrbinary('RETR %s'%(remote_path+k),local_file.write,bufsize) ftp.set_debuglevel(0) local_file.close() print "downloading %s !!!" %(remote_path+k) except: bufsize = 1024 ftp = FTP() ftp.connect(self.ip,self.port) ftp.login(self.user,self.password) local_file = open(local_path+k,'wb') ftp.retrbinary('RETR %s'%(remote_path+k),local_file.write,bufsize) ftp.set_debuglevel(0) local_file.close() print "downloading %s !!!" %(remote_path+k) else: print "%s %s check successfully!!!" %(current_time,remote_path)
客戶配置界面
#!/usr/bin/env python #-*- coding: utf-8 -*- #-author qicongliang- import easyftp,time sys_time = time.time() sys_time_array = time.localtime(sys_time) year = sys_time_array.tm_year mon = sys_time_array.tm_mon day = sys_time_array.tm_mday #*** ##############################################example################################################# # 輸入FTP相關信息,並得出一個FTP實例,名字叫“FTP1” # # - ftp1 = easyftp.ftp('10.172.12.32','21','dqt','dqt') # # # # 調用FTP1的下載功能,並指定FTP目錄與本地目錄。 # # - ftp1.ftp_download('DSShare/release/rh2m/999/2018/11/27/','/mnt/data/grid_test/2018/11/27/rh2m/') # # # # 調用FTP1的校驗功能,並指定需要校驗得FTP目錄與對應的本地目錄。 # # - ftp1.check('/DSShare/release/rh2m/999/2018/11/27/','/mnt/data/grid_test/2018/11/27/rh2m/') # ###################################################################################################### #*** #假如您的目錄是根據時間日期生產的,請用如下格式編輯您的目錄路徑 ################################################################################################################################# # 原本的路徑。 # # - ftp1.check('/DSShare/release/rh2m/999/2018/11/27/','/mnt/data/grid_test/2018/11/27/rh2m/') # # 變形后的路徑 # # - ftp1.ftp_download('DSShare/release/rh2m/999/%s/%s/%s/' %(year,mon,day),'/mnt/data/grid_test/%s/%s/%s/rh2m/' %(year,mon,day))# ################################################################################################################################# #################################請在下面修改您的相關信息######################################### #請輸入你的FTP信息(IP,端口,用戶,密碼) ftp1 = easyftp.ftp('172.16.1.199','21','test','tqw961110') #請輸入需要下載的目錄(FTP目錄,本地目錄) #ftp1.ftp_download('/test/2018/11/27/','/usr/local/ftp_file/test/2018/11/27/') #請輸入需要上傳的目錄(FTP目錄,本地目錄) ftp1.ftp_upload('/test2/','/usr/local/ftp_file/test2/') #請輸入需要校驗的目錄(FTP目錄,本地目錄) #ftp1.check('/test/2018/11/27/','/usr/local/ftp_file/test/2018/11/27/')
下面附上python在FTP中的一些操作
from ftplib import FTP # 加載ftp模塊 ftp = FTP() # 獲取FTP對象 ftp.set_debuglevel(2) # 打開調試級別2,顯示詳細信息 ftp.connect('IP', PORT) # 連接ftp,server和端口 ftp.login('user', 'password') # 登錄用戶 print(ftp.getwelcome()) # 打印歡迎信息 ftp.cmd('xxx/xxx') # 進入遠程目錄 bufsize = 1024 # 設置緩存區大小 filename='filename.txt' # 需要下載的文件 file_handle=open(filename, 'wb').write # 以寫的模式在本地打開文件 file.retrbinaly('RETR filename.txt', file_handle,bufsize) # 接收服務器上文件並寫入本地文件 ftp.set_debuglevel(0) # 關閉調試模式 ftp.quit # 退出ftp ftp相關的命令操作 ftp.cwd(pathname) # 設置FTP當前操作的路徑 ftp.dir() # 顯示目錄下所有目錄的信息 ftp.nlst() # 獲取目錄下的文件 ftp.mkd(pathname) # 新建遠程目錄 ftp.rmd(dirname) # 刪除遠程目錄 ftp.pwd() # 返回當前所在位置 ftp.delete(filename) # 刪除遠程文件 ftp.rename(fromname, toname) #將fromname改為toname ftp.storbinaly('STOR filename.txt',file_handel,bufsize) # 上傳目標文件 ftp。retrbinary('RETR filename.txt',file_handel,bufsize) # 下載FTP文件