-1

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?

Alex Nikonov
  • 1
  • 1
  • 3

1 Answers1

2

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.

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • Your code gives ERROR: Traceback (most recent call last): File "D:/pyt/venv/test.py", line 13, in ftp.retrbinary("RETR " + filename ,open(filename, 'ab').write, **restarg) ftplib.error_perm: 550 Failed to open file. – Alex Nikonov Jun 23 '19 at 18:17
  • I fixed the quoting whilst you typed that. The error looks like your example file name doesn't exist on the server, though. – tripleee Jun 23 '19 at 18:19
  • Thank you very much. How can I track the% of the downloaded file? print? and is there a connection? – Alex Nikonov Jun 23 '19 at 18:24
  • Instead of the `open(...).write` you could pass in a more complex callback which will get called for every block; the default block size is 8192. I don't understand your second question; but anyway, (maybe accept this answer, or post one of your own and accept that, and) start a new question if you have a new question. – tripleee Jun 23 '19 at 18:30
  • How to write a file from a computer to an FTP server? – Alex Nikonov Jun 24 '19 at 04:49
  • this code downloads a file from ftp. And I need to write a file to an FTP server. – Alex Nikonov Jun 24 '19 at 05:45
  • Again, if you have a new question, you are much more likely to get an answer if you post it as an actual question. https://unix.stackexchange.com/questions/91602/is-there-any-ftp-command-to-resume-upload alleges that you should be able to use REST with STOR. Googling this took me 20 seconds tops. – tripleee Jun 24 '19 at 06:08
  • ftp.login("ftp-user", "Qw123456") ftp.cwd(path) if os.path.exists(filename): restarg = {'rest': str(os.path.getsize(filename))} else: restarg = {} ftp.storbinary("STOR " + filename, open(filename, 'ab').write, **restarg) print("End\n") ftp.quit() AttributeError: 'builtin_function_or_method' object has no attribute 'read' – Alex Nikonov Jun 24 '19 at 10:05
  • This question shows in more detail how to hook in a progress indicator while downloading: https://stackoverflow.com/questions/51684008/show-ftp-download-progress-in-python-progressbar (it was my first Google result for `ftplib download progress`). – tripleee Jun 24 '19 at 11:29