Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Progress bar with downloading using urlopen

Tags:

python

url

urllib

My requirement needs me to download a package from a http url and see the progress of the same.

i have below code written

import subprocess
from urllib import urlopen


  class MyClass(object):
  '''
  classdocs
  '''

def url_to_download(self):
    url_for_download = "someurl"
    file = "somefilename"
    print "downloading with urllib"

    response = urlopen(url_for_download)
    CHUNK = 16 * 1024
    with open(file, 'wb') as f:
        while True:
            chunk = response.read(CHUNK)
            cmd = "ls -ltrh" + " " +file + " "+ "|"+ "awk '{print $5}'"
            p = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
            (output, err) = p.communicate()
            print "the download progress is" + " " + str(output)
            if not chunk:
                break
            f.write(chunk)


    if __name__ == "__main__":
     download =  MyClass()
      download.number_of_files()
      download.url_to_download()

As you could see i have used basically linux commands to see the progress of the download.Is there any way with minimal changes to the codes i can have a horizontal progress details for the download. thanks alot in advance

like image 835
vishnu vasudev Avatar asked Nov 05 '25 20:11

vishnu vasudev


1 Answers

Function urlretrieve has a reporthook callback — i.e. you pass a function that urlretrieve calls with 3 arguments.

As for progress bar, you can use any progress module a PyPI. See, for example, tqdm.

like image 108
phd Avatar answered Nov 07 '25 11:11

phd



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!