python实现ftp上下传,支持断点续传。
来源:互联网 发布:绿茶软件园php源码 编辑:程序博客网 时间:2024/04/30 10:57
#!/usr/bin/python#encoding=utf-8 from ftplib import FTP import sys import os.path
#Ftp的类实现
class MyFTP(FTP): ''' conncet to FTP Server ''' def __init__(self): print 'make a object' def ConnectFTP(self,remoteip,remoteport,loginname,loginpassword): ftp=MyFTP() try: ftp.connect(remoteip,remoteport,600) print 'success' except Exception, e: print >> sys.stderr, "conncet failed1 - %s" % e return (0,'conncet failed') else: try: ftp.login(loginname,loginpassword) print('login success') except Exception, e: print >>sys.stderr, 'login failed - %s' % e return (0,'login failed') else: print 'return 1' return (1,ftp) def download(self,remoteHost,remotePort,loginname,loginpassword,remotePath,localPath): #connect to the FTP Server and check the return res = self.ConnectFTP(remoteHost,remotePort,loginname,loginpassword) if(res[0]!=1): print >>sys.stderr, res[1] sys.exit() #change the remote directory and get the remote file size ftp=res[1] ftp.set_pasv(0) dires = self.splitpath(remotePath) if dires[0]: ftp.cwd(dires[0]) # change remote work dir remotefile=dires[1] # remote file name print dires[0]+' '+ dires[1] fsize=ftp.size(remotefile) if fsize==0 : # localfime's site is 0 return #check local file isn't exists and get the local file size lsize=0L if os.path.exists(localPath): lsize=os.stat(localPath).st_size if lsize >= fsize: print 'local file is bigger or equal remote file' return blocksize=1024 * 1024 cmpsize=lsize ftp.voidcmd('TYPE I') conn = ftp.transfercmd('RETR '+remotefile,lsize) lwrite=open(localPath,'ab') while True: data=conn.recv(blocksize) if not data: break lwrite.write(data) cmpsize+=len(data) #print '\b'*30,'download process:%.2f%%'%(float(cmpsize)/fsize*100), print '\n','download process:%.2f%%'%(float(cmpsize)/fsize*100), lwrite.close() ftp.voidcmd('NOOP') ftp.voidresp() conn.close() ftp.quit() def upload(self,remotehost,remoteport,loginname,loginpassword,remotepath,localpath,callback=None): if not os.path.exists(localpath): print "Local file doesn't exists" return self.set_debuglevel(2) res=self.ConnectFTP(remotehost,remoteport,loginname,loginpassword) if res[0]!=1: print res[1] sys.exit() ftp=res[1] remote=self.splitpath(remotepath) ftp.cwd(remote[0]) rsize=0L try: rsize=ftp.size(remote[1]) except: pass if (rsize==None): rsize=0L lsize=os.stat(localpath).st_size print('rsize : %d, lsize : %d' % (rsize, lsize)) if (rsize==lsize): print 'remote filesize is equal with local' return if (rsize<lsize): localf=open(localpath,'rb') localf.seek(rsize) ftp.voidcmd('TYPE I') datasock='' esize='' try: print(remote[1]) datasock,esize=ftp.ntransfercmd("STOR "+remote[1],rsize) except Exception, e: print >>sys.stderr, '----------ftp.ntransfercmd-------- : %s' % e return cmpsize=rsize while True: buf=localf.read(1024 * 1024) if not len(buf): print '\rno data break' break datasock.sendall(buf) if callback: callback(buf) cmpsize+=len(buf) print '\b'*30,'uploading %.2f%%'%(float(cmpsize)/lsize*100), if cmpsize==lsize: print '\rfile size equal break' break datasock.close() print 'close data handle' localf.close() print 'close local file handle' ftp.voidcmd('NOOP') print 'keep alive cmd success' ftp.voidresp() print 'No loop cmd' ftp.quit() def splitpath(self,remotepath): position=remotepath.rfind('/') return (remotepath[:position+1],remotepath[position+1:])#下面是使用代码ftpClient = MyFTP();ftpClient.download('10.61.101.151',21,'test','test','/home/test/X86_GDB','D:\Pmp\X86_GDB');ftpClient.upload('10.61.101.151',21,'test','test','/home/test/123','D:\Pmp\g123');
0 0
- python实现ftp上下传,支持断点续传。
- python ftp上传下载文件,支持断点续传
- Delphi实现Ftp客户端下载(支持断点续传,多线程传输)
- Delphi实现Ftp客户端下载(支持断点续传,多线程传输)
- c#上传下载ftp(支持断点续传)
- c#上传下载ftp(支持断点续传)
- c#上传下载ftp(支持断点续传)
- c#上传下载ftp(支持断点续传)
- c#上传下载ftp(支持断点续传)
- C# 上传下载ftp(支持断点续传)
- WinInet 实现ftp断点续传
- C++实现FTP断点续传
- FTP客户端实现断点续传
- java实现ftp断点续传
- FTP实现断点续传
- FTP实现断点续传
- 用apache FTP client实现FTP客户端--支持断点续传和中文文件 .
- ftp协议实现多线程断点续传
- [Leetcode]Move Zeroes
- InternetDVB
- AndroidValidator使用
- extern "c" __declspec(dllimport) __declspec(dllexport)
- IOS中NSPredicate的用法
- python实现ftp上下传,支持断点续传。
- 10.10笔记,android通知栏兼容性,matches the given name 'android:Widget.Material.Button.Colored'.
- JQuery控制只输入数字,关闭输入法
- QQ飞车
- A9处理器代工厂商不同所带来的性能差异,苹果的官方回应
- CISCO学习笔记(二)IP地址介绍及VLSM
- PHP的输出缓冲区
- struts升级到2.3出现各种问题
- Attribute 和 Parameter 的区别