我需要手动中断 FTP 上传,然后测试是否可以恢复上传。我正在使用 Python 的 ftplib 模块。
我试过下面的代码:
# Consider I have logged in using valid ftp user
# File is of 20 MB
counter = 0
file_name = 'test.dat'
ftp_dir = 'test_ftp_dir'
with open(file_address, 'rb') as file:
ftp.set_debuglevel(2)
ftp.cwd(ftp_dir)
ftp.voidcmd('TYPE I')
with ftp.transfercmd(f'STOR {file_name}', None) as conn:
while True:
# Read 1 MB
buf = file.read(1000000)
if not buf:
break
conn.sendall(buf)
counter += 1
if counter == 5:
# Stop after 5 MB
LOG.info("STEP-3: Abort client transfer")
break
# Reading file again and logging again using the ftp user
with open(file_address, 'rb') as file:
ftp.set_debuglevel(2)
ftp.cwd(ftp_dir)
ftp.voidcmd('TYPE I')
ftp.storbinary(f'STOR {file_name}', file, rest=ftp.size(file_name))
它不是从 5 MB 重新开始上传,而是在附加到原始文件的同时发送完整文件。假设我发送了 5 MB 的文件,那么我可以看到 5 MB 的文件,当我尝试恢复它时,它会发送整个 20 MB 的文件,使其成为总共 25 MB 的文件。请帮我解决这个问题。谢谢。
在开始传输之前,您必须将源本地文件查找到重新启动位置:
# Reading file again and logging again using the ftp user
with open(file_address, 'rb') as file:
rest = ftp.size(file_name)
file.seek(rest)
ftp.cwd(ftp_dir)
ftp.storbinary(f'STOR {file_name}', file, rest=rest)
ftplib 不会为您寻找该文件,这会限制它的使用。在某些情况下,您不希望它寻找。还有一些类似文件的对象不支持查找。
FTP断点上传的完整代码请参见:
Python ftplib FTP传输文件上传中处理断开连接 https://stackoverflow.com/q/67140561/850848
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)