2

I'm using requests lib to download a file, i got many info. about the response like: size,type and date. but i need to get the download speed and set a maximum and minimum for it. How could i get downloading speed ?

Here's the code:

import requests import sys link = "https://upload.wikimedia.org/wikipedia/commons/4/47/PNG_transparency_demonstration_1.png" file_name = "downloaded.png" response = requests.get(link, stream=True) with open(file_name, "wb") as f: print "Downloading %s" % file_name response = requests.get(link, stream=True) total_length = int(response.headers.get('content-length')) print response.headers["content-type"] print total_length / 1024, "Kb" print int(response.headers["Age"]) * (10 ** -6), "Sec" print response.headers["date"] if total_length is None: # no content length header f.write(response.content) else: dl = 0 for data in response.iter_content(chunk_size=4096): dl += len(data) f.write(data) done = int(50 * dl / total_length) sys.stdout.write("\r[%s%s]" % ('=' * done, ' ' * (50-done)) ) sys.stdout.flush() 

and here's the output:

Downloading downloaded.png image/png 213 Kb 0.054918 Sec Wed, 19 Oct 2016 08:43:47 GMT [==================================================] 
2
  • Man, I wanna get the download speed in kb i don't need the progress bar . Commented Oct 19, 2016 at 8:48
  • 2
    The linked question contains all the information you need, even if it also contains information you do not need. Commented Oct 19, 2016 at 8:52

1 Answer 1

2

I just added import time, the start variable and replaced the sys.stdout.write line with the one from: How to measure download speed and progress using requests?

import requests import sys import time link = "https://upload.wikimedia.org/wikipedia/commons/4/47/PNG_transparency_demonstration_1.png" file_name = "downloaded.png" start = time.clock() response = requests.get(link, stream=True) with open(file_name, "wb") as f: print "Downloading %s" % file_name response = requests.get(link, stream=True) total_length = int(response.headers.get('content-length')) print response.headers["content-type"] print total_length / 1024, "Kb" print int(response.headers["Age"]) * (10 ** -6), "Sec" print response.headers["date"] if total_length is None: # no content length header f.write(response.content) else: dl = 0 for data in response.iter_content(chunk_size=4096): dl += len(data) f.write(data) done = int(50 * dl / total_length) sys.stdout.write("\r[%s%s] %s bps" % ('=' * done, ' ' * (50-done), dl//(time.clock() - start))) sys.stdout.flush() 
Sign up to request clarification or add additional context in comments.

3 Comments

Bro, could you explain to me how the speed counted in this line
dl//(time.clock() - start)
Sure, time.clock() returns the processor time in seconds, we get it at start and we store it in the variable start. Then during the iteration in the progress bar we get time.clock() again so we can calculate the delta (current time clock - time clock at start). The delta is the number of seconds between the start of the download and the current time, we can divide the total bytes received (the variable dl) with the delta to roughly calculate the bytes received per second

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.