所以你需要寻找文件并发送一个 REST 命令告诉服务器从正确的位置下载。
这里的代码会一直尝试,直到完成上传简历,调试也已开启,因此您可以关注:
#!/usr/bin/env python3
import ftplib
import os
import sys
import time
import socket
class FtpUploadTracker:
sizeWritten = 0
totalSize = 0.0
lastShownPercent = 0
def __init__(self, totalSize):
self.totalSize = totalSize
def handle(self, block):
self.sizeWritten += 1024
percentComplete = round((self.sizeWritten / self.totalSize) * 100)
if (self.lastShownPercent != percentComplete):
self.lastShownPercent = percentComplete
print(str(percentComplete) + "% complete ramaing: " + str(self.totalSize - self.sizeWritten), flush=True)
if __name__ == "__main__":
Server="servername.com"
Username="username"
Password="secret password"
filename = "/path/to/folder"
Directory="/path/on/server"
tries = 0
done = False
print("Uploading " + str(filename) + " to " + str(Directory), flush=True)
while tries < 50 and not done:
try:
tries += 1
with ftplib.FTP(Server) as ftp:
ftp.set_debuglevel(2)
print("login", flush=True)
ftp.login(Username, Password)
# ftp.set_pasv(False)
ftp.cwd(Directory)
with open(filename, 'rb') as f:
totalSize = os.path.getsize(filename)
print('Total file size : ' + str(round(totalSize / 1024 / 1024 ,1)) + ' Mb', flush=True)
uploadTracker = FtpUploadTracker(int(totalSize))
# Get file size if exists
files_list = ftp.nlst()
print(files_list, flush=True)
if os.path.basename(filename) in files_list:
print("Resuming", flush=True)
ftp.voidcmd('TYPE I')
rest_pos = ftp.size(os.path.basename(filename))
f.seek(rest_pos, 0)
print("seek to " + str(rest_pos))
uploadTracker.sizeWritten = rest_pos
print(ftp.storbinary('STOR ' + os.path.basename(filename), f, blocksize=1024, callback=uploadTracker.handle, rest=rest_pos), flush=True)
else:
print(ftp.storbinary('STOR ' + os.path.basename(filename), f, 1024, uploadTracker.handle), flush=True)
done = True
except (BrokenPipeError, ftplib.error_temp, socket.gaierror) as e:
print(str(type(e)) + ": " + str(e))
print("connection died, trying again")
time.sleep(30)
print("Done")
魔法线是:
print(ftp.storbinary('STOR ' + os.path.basename(filename), f, blocksize=1024, callback=uploadTracker.handle, rest=rest_pos), flush=True)
其中有rest=rest_pos。
如果给定了可选的 rest,则向服务器发送一个 REST 命令,
将 rest 作为参数传递。 rest 通常是一个字节偏移量
请求的文件,告诉服务器重新开始发送文件的字节
在请求的偏移量处,跳过初始字节。但是请注意
RFC 959 只要求其余是包含字符的字符串
在 ASCII 码 33 到 ASCII 码 126 的可打印范围内。
因此,transfercmd() 方法将 rest 转换为字符串,但没有
检查字符串的内容。如果服务器没有
识别 REST 命令,将引发 error_reply 异常。
如果发生这种情况,只需调用 transfercmd() 而不使用 rest 参数
Source
还有一些代码taken from here