Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8467627
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T15:41:59+00:00 2026-06-10T15:41:59+00:00

I am downloading files over http and displaying the progress using urllib and the

  • 0

I am downloading files over http and displaying the progress using urllib and the following code – which works fine:

import sys
from urllib import urlretrieve

urlretrieve('http://example.com/file.zip', '/tmp/localfile', reporthook=dlProgress)

def dlProgress(count, blockSize, totalSize):
  percent = int(count*blockSize*100/totalSize)
  sys.stdout.write("\r" + "progress" + "...%d%%" % percent)
  sys.stdout.flush()

Now I would also like to restart the download if it is going too slow (say less than 1MB in 15 seconds). How can I achieve this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T15:42:01+00:00Added an answer on June 10, 2026 at 3:42 pm

    This should work.
    It calculates the actual download rate and aborts if it is too low.

    import sys
    from urllib import urlretrieve
    import time
    
    url = "http://www.python.org/ftp/python/2.7.3/Python-2.7.3.tgz" # 14.135.620 Byte
    startTime = time.time()
    
    class TooSlowException(Exception):
        pass
    
    def convertBToMb(bytes):
        """converts Bytes to Megabytes"""
        bytes = float(bytes)
        megabytes = bytes / 1048576
        return megabytes
    
    
    def dlProgress(count, blockSize, totalSize):
        global startTime
    
        alreadyLoaded = count*blockSize
        timePassed = time.time() - startTime
        transferRate = convertBToMb(alreadyLoaded) / timePassed # mbytes per second
        transferRate *= 60 # mbytes per minute
    
        percent = int(alreadyLoaded*100/totalSize)
        sys.stdout.write("\r" + "progress" + "...%d%%" % percent)
        sys.stdout.flush()
    
        if transferRate < 4 and timePassed > 2: # download will be slow at the beginning, hence wait 2 seconds
            print "\ndownload too slow! retrying..."
            time.sleep(1) # let's not hammer the server
            raise TooSlowException
    
    def main():
        try:
            urlretrieve(url, '/tmp/localfile', reporthook=dlProgress)
    
        except TooSlowException:
            global startTime
            startTime = time.time()
            main()
    
    if __name__ == "__main__":
        main()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm making a program which downloads files over http. I've got it downloading, however
I have an application playing remote MP3 files over HTTP using the JLayer/BasicPlayer libraries.
I am downloading files from my server, saving them to device, and displaying them
I am downloading files through IFRAME method described here: http://encosia.com/2007/02/23/ajax-file-downloads-and-iframes/ And am showing progress
I would like to show a progress view while my app is downloading files
I have the following grabber that is downloading empty files and saving them to
I am using libcurl I have my downloading of files inside of a class,
I am consuming various XML-over-HTTP web services returning large XML files (> 2MB). What
Hey all! I am trying to post a file over Http using Java 1.4.2.
I'm using wget to download a set of files via HTTP, using one wget

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.