patternpythonMinor
Internet Speed Calculator
Viewed 0 times
internetcalculatorspeed
Problem
This program calculates up and downspeed of a given connection for as long as it is running.
I'd appreciate it if your dropped any hints about turning this into a desktop widget.
#upspeed/downspeed indicator
#written by Chubak -> aurlito@riseup.net
import requests
import time
import os
def get_seconds(datetime):
time = datetime.split(' ')[3].split(':')
time_hours = int(time[0])
time_minutes = int(time[1])
time_seconds = int(time[2])
return time_hours*360 + time_minutes*60 + time_seconds
def downspeed():
url = "http://speedtest.ftp.otenet.gr/files/test100k.db"
current_seconds = get_seconds(time.asctime())
file = requests.get(url)
headers = file.headers
file_size = int(headers['Content-Length'])/1000
dl_seconds = get_seconds(time.asctime())
time_difference = dl_seconds - current_seconds
return round(file_size / time_difference)
def upspeed():
current_seconds = get_seconds(time.asctime())
dummy_file = os.path.join(os.getenv('APPDATA'), 'dummy.txt')
post_url = 'http://httpbin.org/post'
with open(dummy_file, 'wb') as dummy:
for i in range (1500):
dummy.write(str.encode('This is a dummy text. Its sole propose is being uploaded to a server. '))
dummy.close()
files = {'file' : open(dummy_file, 'rb')}
request = requests.post(post_url, data=files)
headers = request.headers
file_size = int(headers['Content-Length'])/1000
dl_seconds = get_seconds(time.asctime())
time_difference = dl_seconds - current_seconds
return round(file_size / time_difference)
if __name__ == '__main__':
while True:
up = None
down = None
if up == None and down == None:
up = upspeed()
down = downspeed()
print('At {0} your Downspeed is: {1}, and your Upspeed is: {2}'.format(time.asctime(), down, up))
time.sleep(5)
up = None
down = NoneI'd appreciate it if your dropped any hints about turning this into a desktop widget.
Solution
Your whole function
You should also report the units of your speeds measured. As it is, they will be KB/s, with the K denoting 1000 (as you divide by that). You should be aware that there is also a different unit, sometimes written KiB/s, where Ki stands for 1024. You should probably use the same definition, which speedtest uses, when it says the file is 100kb big.
get_seconds is not needed at all. You can use the function time.time(), which gives you the seconds since epoch (Jan 1, 1970 0:00:00). Subtracting two such times will yield the number of seconds passed in between. I would also do the time measurement exclusively around the actual download, otherwise you add the (small) overhead of reading the header for the file-size.def downspeed():
url = "http://speedtest.ftp.otenet.gr/files/test100k.db"
start = time.time()
file = requests.get(url)
end = time.time()
time_difference = end - start
file_size = int(file.headers['Content-Length'])/1000
return round(file_size / time_difference)You should also report the units of your speeds measured. As it is, they will be KB/s, with the K denoting 1000 (as you divide by that). You should be aware that there is also a different unit, sometimes written KiB/s, where Ki stands for 1024. You should probably use the same definition, which speedtest uses, when it says the file is 100kb big.
Code Snippets
def downspeed():
url = "http://speedtest.ftp.otenet.gr/files/test100k.db"
start = time.time()
file = requests.get(url)
end = time.time()
time_difference = end - start
file_size = int(file.headers['Content-Length'])/1000
return round(file_size / time_difference)Context
StackExchange Code Review Q#139319, answer score: 5
Revisions (0)
No revisions yet.