resuming download file ftp python3.*

2024/11/13 9:40:40

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?

Answer

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.

https://en.xdnf.cn/q/119372.html

Related Q&A

printing files based on character

I have a directory(data) that contain thousand of files.Each time I want to select three files that are just differ by only one characterAB[C,D,E] and want to perform some computation on the selected t…

Parsing CSV file using Panda

I have been using matplotlib for quite some time now and it is great however, I want to switch to panda and my first attempt at it didnt go so well.My data set looks like this:sam,123,184,2.6,543 winte…

Getting division by zero error with Python and OpenCV

I am using this code to remove the lines from the following image:I dont know the reason, but it gives me as output ZeroDivisionError: division by zero error on line 34 - x0, x1, y0, y1 = (0, im_wb.sha…

Pandas complex calculation based on other columns

I have successfully created new columns based on arithmetic for other columns but now I have a more challenging need to first select elements based on matches of multiple columns then perform math and …

how to generate word from a to z [closed]

Its difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying thi…

How to webscrape all shoes on nike page using python

I am trying to webscrape all the shoes on https://www.nike.com/w/mens-shoes-nik1zy7ok. How do I scrape all the shoes including the shoes that load as you scroll down the page? The exact information I …

Pyo in Python: name Server not defined

I recently installed Pyo, and I entered Python 3.6 and typedfrom pyo import * s = Server().boot() s.start() sf = SfPlayer("C:\Users\myname\Downloads\wot.mp3", speed=1, loop=True).out()but I …

Limited digits with str.format(), and then only when they matter

If were printing a dollar amount, we usually want to always display two decimal digits.cost1, cost2 = 123.456890123456789, 357.000 print {c1:.2f} {c2:.2f}.format(c1=cost1, c2=cost2)shows123.46 357.00…

How is covariance implemented internally in numpy?

This is the definition of a covariance matrix. http://en.wikipedia.org/wiki/Covariance_matrix#DefinitionEach element in the matrix, except in the principal diagonal, (if I am not wrong) simplifies to E…

Pulling excel rows to display as a grid in tkinter

I am imaging fluorescent cells from a 384-well plate and my software spits out a formatted excel analysis of the data (16 rowsx24 columns of images turns into a list of data, with 2 measurements from e…