Skip to content

Instantly share code, notes, and snippets.

@deadbok
Last active August 29, 2015 14:00
Show Gist options
  • Save deadbok/11367324 to your computer and use it in GitHub Desktop.
Save deadbok/11367324 to your computer and use it in GitHub Desktop.
Simple tool to take some statistics of download speed.
import urllib.request
import time
#URL used for test download
URL = ('http://ftp.gnu.org/gnu/binutils/binutils-2.24.tar.gz')
#Times to download the file
NUMBER_OF_DL = 1
#File to save the staticts to
STATS_FILENAME = 'stats.csv'
def report(start, total, speed):
'''Print a report.'''
print('Transfer rate ' + str(speed / 1024.0) + ' MBps.')
#print ('Reading took ' + str(total) + ' seconds, transfer rate ' + str((size / 1024.0) / total) + ' KBPS')
print('Dowwnload speed test logger V. 0.1 by deadbok')
with open(STATS_FILENAME, 'w') as stats_file:
for _i in range(0, NUMBER_OF_DL):
#Open the URL
f = urllib.request.urlopen(URL)
#Get the current time
start = time.time()
#Read all data in a single, blocking operation
data = f.read()
#Get the ime it took
total = time.time() - start
#Calculate the speed
bits = len(data)*8
speed = (bits / 1024.0) / total
#Print a report
report(start, total, speed)
#Close the connection
f.close()
#Save stats to file
stats_file.write(str(speed) + ',')
stats_file.close()
#f = urllib2.urlopen(url)
#start = time.time()
#while True:
# chunk = f.read(4096) # read a chunk
# if not chunk:
# break
#report(start, len(data), 'Chunked')
#<f.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment