从状态文件跟踪 LFTP 下载

Track LFTP download from the status file

我正在通过 python 子进程调用 lftp 命令,并在 lftp 命令中使用 pget 的 -n 标志来设置最大连接数。现在,当下载正在进行时,将创建一个名为 filename.lftp-pget-status 的状态文件,并在下载结束后自动将其删除。

这是 connections = 4

的状态文件的示例输出
$ cat abc_20200619.gz.lftp-pget-status 
size=1837873446
0.pos=459472896
0.limit=459468363
1.pos=1301863572
1.limit=1378405085
2.pos=1735117533
2.limit=1837873446

我需要从状态文件跟踪下载进度。我无法理解内容,因为分区在下载结束时也会减少。我写了下面的公式来计算下载数据的字节数,但我不认为这是正确的方法。

bytes_downloaded = 0.pos + (1.pos-0.limit) + (2.pos-1.limit) + (n.pos-(n-1).limit)

有人知道从状态文件跟踪 lftp 下载吗?

如果我们将成对的“pos”和“limit”值视为 {(Pᵢ, Lᵢ) | 0 ≤ i < n}, where Lₙ₋₁ 是完整的大小,我们可以归因于以下内容。

剩余字节数可以表示为Σᵢ₌₀ⁿ⁻¹(Lᵢ - Pᵢ). So, the number of downloaded bytes can be represented as Lₙ₋₁ - (Σᵢ₌₀ⁿ⁻¹(Lᵢ - Pᵢ)). This expression can be manipulated in many ways, like (Σᵢ₌₀ⁿ⁻¹(Pᵢ - Lᵢ)) + Lₙ₋₁, but the most useful one programmatically is probably (Σᵢ₌₀ⁿ⁻¹(Pᵢ)) - (Σᵢ₌₀ⁿ⁻²(Lᵢ))。示例算法如下:

FILE = open('example.lftp-pget-status', 'r') 
file_lines = FILE.readlines()
downloaded_bytes = 0

# first and last lines are removed and numbers extracted for other lines
file_lines = [int(re.sub(r'.*=', '', line)) for line in file_lines[1:-1]]

downloaded_bytes += sum(file_lines[0::2]) # values with even index (the pos)
downloaded_bytes -= sum(file_lines[1::2]) # values with odd index (the limit)

这个答案来晚了,但希望对搜索的其他人有所帮助。