Issue
On Linux (Ubuntu), when I run wget www.example.com/file.zip -O file.zip
I see a progress bar representing the download progress. As the picture below shows:
Is there a way in Python to retrieve ALL the information I surrounded in red ?
I mean that I would love to retrieve into separate Python variables these information:
- The amout of data that is being downloading
- The speed of download
- The remaining time
Solution
As these information are output to stderr
, so you need to read them from sys.stderr.
We can use select to read the stderr as the output is changing.
FYI, bellow is a example:
# -*- coding: utf-8 -*-
from subprocess import PIPE, Popen
import fcntl
import os
import select
import sys
proc = Popen(['wget', 'http://speedtest.london.linode.com/100MB-london.bin'], stdin = PIPE, stderr = PIPE, stdout = PIPE)
while proc.poll() == None:
fcntl.fcntl(
proc.stderr.fileno(),
fcntl.F_SETFL,
fcntl.fcntl(proc.stderr.fileno(), fcntl.F_GETFL) | os.O_NONBLOCK,
)
buf = ''
while proc.poll() == None:
readx_err = select.select([proc.stderr.fileno()], [], [], 0.1)[0]
if readx_err:
chunk = proc.stderr.read().decode('utf-8')
buf += chunk
if '\n' in buf and '%' in buf and '.' in buf:
print (buf.strip().split())
buf = ''
else:
break
proc.wait()
Answered By - atupal
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.