Skip to content

Instantly share code, notes, and snippets.

@harlowja
Created June 26, 2019 05:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save harlowja/556fc9e0dcece4c677c50c768cc16a2d to your computer and use it in GitHub Desktop.
Save harlowja/556fc9e0dcece4c677c50c768cc16a2d to your computer and use it in GitHub Desktop.
import requests
import time
import sys
urls = [
#"http://releases.ubuntu.com/19.04/ubuntu-19.04-live-server-amd64.iso",
"http://www.ovh.net/files/10Gb.dat",
"http://speedtest-ca.turnkeyinternet.net/1000mb.bin",
]
tries = 3
chunk_size = 512 * 1024
out_fn = "test.bin"
for url in urls:
for i in range(0, tries):
print("Downloading %s (trial #%s) --> %s" % (url, i + 1, out_fn))
r = requests.get(url, stream=True)
perc_shown = 0
with open(out_fn, 'wb') as fh:
start = time.time()
tot_size = 0
expected_size = r.headers['content-length']
#print(" %s expected size" % (expected_size))
out = 0
for chunk in r.iter_content(chunk_size=chunk_size):
if chunk:
fh.write(chunk)
fh.flush()
tot_size += len(chunk)
out += 1
if out == 100:
perc_done = float(tot_size) / float(expected_size)
perc_done = perc_done * 100
sys.stdout.write(" %0.2f%%" % perc_done)
sys.stdout.flush()
out = 0
perc_shown += 1
if perc_shown:
sys.stdout.write("\n")
sys.stdout.flush()
end = time.time()
secs_taken = end - start
print(" %0.3f seconds taken" % secs_taken)
bytes_per_second = tot_size / secs_taken
print(" %0.3f bytes/per second" % bytes_per_second)
kb_per_sec = bytes_per_second / 1024
print(" %0.3f kilobytes/per second" % kb_per_sec)
mb_per_sec = kb_per_sec / 1024
print(" %0.3f megabytes/per second" % mb_per_sec)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment