There is a file (1-7Gb) that you need to pick up. The network periodically falls, so it is necessary to implement the method of resume. For example, in 1 communication session downloaded 20% the network disappeared, 2 session appeared and the download went from 20%, etc. Help please in Python just started to understand. I understand that you can download the file like this
import ftplib
path = ‘/’
filename = ‘100KB.zip’
ftp = ftplib.FTP(“speedtest.tele2.net”)
ftp.login(“anonymous”, “”)
ftp.cwd(path)
ftp.retrbinary(“RETR ” + filename ,open(filename, ‘wb’).write)
print(“. Загрузка успешно окончена!\n”)
ftp.quit()
How to download a file with a missing network?
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 ospath = '/'
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.