Skip to content

Instantly share code, notes, and snippets.

@Ogreman
Last active August 29, 2015 13:57
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 Ogreman/9525649 to your computer and use it in GitHub Desktop.
Save Ogreman/9525649 to your computer and use it in GitHub Desktop.
from __future__ import division
from __future__ import print_function
import requests
import time
from contextlib import closing
import sys
# in bytes
CHUNKS = 1024
def down(url):
start = time.time()
store = []
times = []
with closing(requests.get(url, stream=True)) as r:
dl = 0
while True:
start_loop = time.time()
for chunk in r.iter_content(chunk_size=CHUNKS):
if chunk:
dl += len(chunk)
store.append(chunk)
end_loop = time.time()
if int(end_loop - start_loop) >= 1:
spd = dl // (time.time() - start)
times.append(spd)
break
else:
break
return sum(times) / len(times)
if __name__ == '__main__':
url = raw_input("Enter url:")
print("Complete! Avg: {spd} Kbyte/sec.".format(spd=down(url) / 1024))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment