The retrbinary
command accepts an optional rest
argument which should contain a string indicating the byte offset at which to restart the transfer. This is described in more detail in the transfercmd
documentation; several of the file-transfer commands support this optional argument.
This facility is optional, so the server might not support it; you should be prepared to handle an error return, and fall back to fetching the entire file (or aborting).
Your calling code should of course be set up to append to the unfinished file, rather than overwrite it!
Untested, not at my computer:
import ftplib
import os
path = '/'
filename = '100KB.zip'
ftp = ftplib.FTP("speedtest.tele2.net")
ftp.login("anonymous", "")
ftp.cwd(path)
if os.path.exists(filename):
restarg = {'rest': str(os.path.getsize(filename))}
else:
restarg = {}
ftp.retrbinary("RETR " + filename ,open(filename, 'ab').write, **restarg)
print("untranslated string in some Slavic language?\n")
ftp.quit()
The Python **kwargs
notation allows us to use a dictionary to pass keyword arguments in a function call. We pass an empty dictionary (no additional keyword arguments) if the file doesn't already exist, and otherwise a dict
containing the keyword 'rest'
and its value. In both cases we use a file mode 'ab'
which will append to an existing binary file, or simply create a new binary file otherwise, and open it for writing.