PYTHON单任务FTP断点续传程序
自己寫的單任務FTP斷點續傳程序,新創建的任務會存到日志文件中(可存儲多個任務的日志),異常中斷重啟時可以選擇指定任務繼續下載。程序比較簡單,也可以說比較簡陋,有些問題并未細致的考慮,比如磁盤空間不足的情況。SO,僅是作為一個簡單的學習例程(給自己,也給那些認為可以學習學習的朋友們,或許想偷懶的人還可以拿去簡單的應用)!
?
myctrl.py#-*-encoding:gbk-*- #Author:沒車步行 #Email:lu_zhouyu@hotmail.com #Edit:2009-4-7 #Copyright:轉載請注明出處 import sys import base64 import shelve import myftplib import osshlfp='task.dat'class mytask:"""shlf:任務隊列remoteip:遠程服務器IP,默認localhostremoteport:遠程服務器端口,默認21remotepath:遠程服務器路徑,默認根目錄remotefilename:遠程服務器文件,必填項localpath:本地路徑,默認程序目錄localfilename:本地文件,默認同遠程服務器文件taskcode:任務代碼,用于判斷任務是否已存在,假設性條件無限制loginname:遠程服務器登錄名loginpassword:遠程服務器登錄密碼"""shlf=shelve.open(shlfp)remoteip='localhost'remoteport='21'remotepath=localpath=''localfilename=remotefilename=''taskcode=''loginname=''loginpassword=''def __init__(self):self.printtask()def maketask(self,remotefilename,localfilename,**kwargs):self.remotefilename=remotefilenameif localfilename:self.localfilename=localfilenameelse:self.localfilename=remotefilenamefor k in kwargs.keys():v=kwargs[k]if k=='remoteip' and v!='':self.remoteip=velif k=='remoteport' and v!='':self.remoteport=velif k=='remotepath' and v!='':self.remotepath=velif k=='localpath' and v!='':self.localpath=velif k=='loginname' and v!='':self.loginname=velif k=='loginpassword' and v!='':self.loginpassword=vself.starttask()def removetask(self,taskid):del self.shlf[taskid]def starttask(self):taskid='1'if len(self.shlf):taskid=str(int(max(self.shlf.keys()))+1)taskval={}taskval['remoteip']=self.remoteiptaskval['remoteport']=self.remoteporttaskval['remotepath']=self.remotepathtaskval['remotefilename']=self.remotefilenametaskval['localpath']=self.localpathtaskval['localfilename']=self.localfilenametaskval['taskcode']=self.taskcodetaskval['loginname']=self.loginnametaskval['loginpassword']=self.loginpasswordself.shlf[taskid]=taskvalmyftplib.mywork(self.remoteip,self.remoteport,self.remotepath,self.remotefilename,self.localpath,self.localfilename,self.loginname,self.loginpassword,0)del self.shlf[taskid]self.shlf.close()def restarttask(self,taskid):taskval=self.shlf[taskid]remoteip=taskval['remoteip']remoteport=taskval['remoteport']remotepath=taskval['remotepath']remotefilename=taskval['remotefilename']localpath=taskval['localpath']localfilename=taskval['localfilename']loginname=taskval['loginname']loginpassword=taskval['loginpassword']myftplib.mywork(remoteip,remoteport,remotepath,remotefilename,localpath,localfilename,loginname,loginpassword,1)del self.shlf[taskid]self.shlf.close()def printtask(self):print '%-6s|%-15s|%s'%('任務ID','服務器地址','下載文件名')print '%s'%('-'*38)for t in self.shlf.keys():v=self.shlf[t]print '%-6s|%-15s|%s'%(t,v['remoteip'],v['remotefilename'])print ''if __name__=='__main__':mytask=mytask()if len(mytask.shlf):chk=raw_input('重新啟動已存在的下載任務(Y-是/N-否):')if chk.strip().upper()=='Y':while 1:tid=raw_input('輸入任務ID:')tid=tid.strip()if mytask.shlf.has_key(tid):mytask.restarttask(tid)breakprint '輸入有誤!',sys.exit()remoteip=raw_input("遠程地址(localhost):")remoteip=remoteip.strip()remoteport=raw_input("遠程端口(21):")remoteport=remoteport.strip()remotepath=raw_input("遠程路徑(默認為根目錄):")remotepath=remotepath.strip()remotefilename=raw_input("遠程文件名:")remotefilename=remotefilename.strip()loginname=raw_input("登錄名:")loginname=loginname.strip()loginpassword=raw_input("登錄密碼:")loginpassword=loginpassword.strip()localfilename=raw_input("本地重命名(默認與遠程文件同名):")localfilename=localfilename.strip()localpath=raw_input("本地路徑(不存在自動創建):")localpath=localpath.strip()mytask.maketask(remotefilename,/localfilename,/remoteip=remoteip,/remoteport=remoteport,/remotepath=remotepath,/localpath=localpath,/loginname=loginname,/loginpassword=loginpassword)
?
myftplib.py#-*-encoding:gbk-*- #Author:沒車步行 #Email:lu_zhouyu@hotmail.com #Edit:2009-4-7 #Copyright:轉載請注明出處 from ftplib import FTP import os import sysclass MyFTP(FTP):#對FTP的繼承#繼承父類中的方法,在子類中可以直接調用#重載父類中retrbinary的方法def retrbinary(self, cmd, callback, fsize=0,rest=0):blocksize=1024cmpsize=restself.voidcmd('TYPE I')conn = self.transfercmd(cmd, rest)#此命令實現從指定位置開始下載,以達到續傳的目的while 1:if fsize:print '/b'*30,'下載進度:%.2f%%'%(float(cmpsize)/fsize*100),data = conn.recv(blocksize)if not data:breakcallback(data)cmpsize+=blocksizeconn.close()return self.voidresp()def ConnectFTP(remoteip,remoteport,loginname,loginpassword):ftp=MyFTP()try:ftp.connect(remoteip,remoteport)except:return (0,'connect failed!')else:try:ftp.login(loginname,loginpassword)except:return (0,'login failed!')else:return (1,ftp)def mywork(remoteip,remoteport,remotepath,remotefilename,localpath,localfilename,loginname,loginpassword,reloadflag):res=ConnectFTP(remoteip,remoteport,loginname,loginpassword)if res[0]!=1:print res[1]sys.exit()ftp=res[1]ftp.set_pasv(0) #到這一部出現連接超時請償試設置非0值if remotepath:#如果文件不在FTP根目錄,則需要指定下一級目錄ftp.cwd(remotepath)fsize=ftp.size(remotefilename)#獲取遠程文件大小print '目標文件大小:',fsizeif fsize==0:returnlocalfullpath=localfilenameif localpath:localfullpath=localpath+'/'+localfilenameif not os.path.exists(localpath):os.makedirs(localpath)if reloadflag and os.path.isfile(localfullpath):rest=os.stat(localfullpath).st_size#獲取本地文件的大小if rest:if rest==fsize:print '遠程文件大小與本地文件大小相同,刪除任務!'returnprint '/b'*30,'已經下載:%.2f%%'%(float(rest)/fsize*100),raw_input('回車繼續....')ftp.retrbinary('RETR '+remotefilename,open(localfullpath,'ab').write,fsize,rest)else:ftp.retrbinary('RETR '+remotefilename,open(localfullpath,'wb').write,fsize)print '完成文件大小:',os.stat(localfullpath).st_size
?
轉載請注明出處?
總結
以上是生活随笔為你收集整理的PYTHON单任务FTP断点续传程序的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 实战 Mantle 解析界面app 科技
- 下一篇: python高级2