1xg93976uq 发表于 2016-6-9 12:03:25

python ftp上传下载文件,支持断点续传

功能:ftp断点上传下载
仅供参考:

#encoding=utf-8
'''
Created on 2012-3-14
@author: cooler
'''
from ftplib import FTP
import sys
import os.path
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:
print "conncet failed1"
return (0,'conncet failed')
else:
try:
ftp.login(loginname,loginpassword)
print 'login success'
except:
print 'login failed'
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!=1):
print res
sys.exit()
#change the remote directory and get the remote file size
ftp=res
ftp.set_pasv(0)
dires = self.splitpath(remotePath)
if dires:
ftp.cwd(dires)
remotefile=dires
print dires+' '+ dires
fsize=ftp.size(remotefile)
if fsize==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
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),
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!=1:
print res
sys.exit()
ftp=res
remote=self.splitpath(remotepath)
ftp.cwd(remote)
rsize=0L
try:
rsize=ftp.size(remote)
except:
pass
if (rsize==None):
rsize=0L
lsize=os.stat(localpath).st_size
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=ftp.ntransfercmd("STOR "+remote,rsize)
cmpsize=rsize
while True:
buf=localf.read(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)

if __name__=='__main__':
lf = MyFTP()
lf.ConnectFTP("192.168.100.237","21","cooler","123123")
lf.download("192.168.100.237","21","cooler","123123","/tmp/cooler/boke.rar","C:/cooler/boke.rar")
lf.upload("192.168.100.237","21","cooler","123123","/tmp/cooler/boke1.rar","C:/cooler/boke.rar")
页: [1]
查看完整版本: python ftp上传下载文件,支持断点续传