From 139a231e46c5565d3c8463a64b7dd9607a9ae8db Mon Sep 17 00:00:00 2001 From: Justin Austin Date: Sat, 22 Jul 2017 20:55:30 -0400 Subject: [PATCH 1/5] linted with flake8 --- fast_com.py | 373 ++++++++++++++++++-------------------- fast_com.pyc | Bin 4795 -> 4905 bytes fast_com_example_usage.py | 9 +- sab_proto.py | 5 +- 4 files changed, 181 insertions(+), 206 deletions(-) diff --git a/fast_com.py b/fast_com.py index 23abe85..c871f53 100644 --- a/fast_com.py +++ b/fast_com.py @@ -1,8 +1,7 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 ''' -Python CLI-tool (without need for a GUI) to measure Internet speed with fast.com - +Python CLI-tool to measure Internet speed with fast.com ''' @@ -11,212 +10,190 @@ import urllib import urllib2 import sys -#import jsbeautifier import time -#import threading from threading import Thread -def gethtmlresult(url,result,index): - ''' - get the stuff from url in chuncks of size CHUNK, and keep writing the number of bytes retrieved into result[index] - ''' - #print url, index, result[index] - req = urllib2.urlopen(url) - CHUNK = 100 * 1024 - i=1 - while True: - chunk = req.read(CHUNK) - if not chunk: break - result[index] = i*CHUNK - i=i+1 +def gethtmlresult(url, result, index): + ''' + get the stuff from url in chuncks of size CHUNK, and keep writing + the number of bytes retrieved into result[index] + ''' + req = urllib2.urlopen(url) + CHUNK = 100 * 1024 + i = 1 + while True: + chunk = req.read(CHUNK) + if not chunk: + break + result[index] = i * CHUNK + i = i + 1 + def application_bytes_to_networkbits(bytes): - # convert bytes (at application layer) to bits (at network layer) - return bytes * 8 * 1.0415 - # 8 for bits versus bytes - # 1.0416 for application versus network layers + # convert bytes (at application layer) to bits (at network layer) + # 8 for bits versus bytes + # 1.0416 for application versus network layers + return bytes * 8 * 1.0415 def findipv4(fqdn): - ''' - find IPv4 address of fqdn - ''' - import socket - ipv4 = socket.getaddrinfo(fqdn, 80, socket.AF_INET)[0][4][0] - return ipv4 + ''' + find IPv4 address of fqdn + ''' + import socket + ipv4 = socket.getaddrinfo(fqdn, 80, socket.AF_INET)[0][4][0] + return ipv4 + def findipv6(fqdn): - ''' - find IPv6 address of fqdn - ''' - import socket - ipv6 = socket.getaddrinfo(fqdn, 80, socket.AF_INET6)[0][4][0] - return ipv6 + ''' + find IPv6 address of fqdn + ''' + import socket + ipv6 = socket.getaddrinfo(fqdn, 80, socket.AF_INET6)[0][4][0] + return ipv6 def fast_com(verbose=False, maxtime=15, forceipv4=False, forceipv6=False): - ''' - verbose: print debug output - maxtime: max time in seconds to monitor speedtest - forceipv4: force speed test over IPv4 - forceipv6: force speed test over IPv6 - ''' - # go to fast.com to get the javascript file - url = 'https://fast.com/' - try: - urlresult = urllib.urlopen(url) - except: - # no connection at all? - return 0 - response = urlresult.read() - for line in response.split('\n'): - # We're looking for a line like - # - if line.find('script src') >= 0: - #print line - jsname = line.split('"')[1] # At time of writing: '/app-40647a.js' - - - # From that javascript file, get the token: - url = 'https://fast.com' + jsname - if verbose: print "javascript url is", url - urlresult = urllib.urlopen(url) - allJSstuff = urlresult.read() # this is a obfuscated Javascript file - ''' - # OLD STUFF ... beautiful, but needs the js-beautifier module, which was a non-stardard requirement - res = jsbeautifier.beautify(allJSstuff) # ... so un-obfuscate it - for line in res.split('\n'): - if line.find('token:') >= 0: - token = line.split('"')[1] - if verbose: print "token is", token - ''' - - ''' - We're searching for the "token:" in this string: - .dummy,DEFAULT_PARAMS={https:!0,token:"YXNkZmFzZGxmbnNkYWZoYXNkZmhrYWxm",urlCount:3,e - ''' - for line in allJSstuff.split(','): - if line.find('token:') >= 0: - if verbose: print "line is", line - token = line.split('"')[1] - if verbose: print "token is", token - if token: - break - - # https://api.fast.com/netflix/speedtest?https=true&token=YXNkZmFzZGxmbnNkYWZoYXNkZmhrYWxm&urlCount=3 - # https://api.fast.com/netflix/speedtest?https=true&token=YXNkZmFzZGxmbnNkYWZoYXNkZmhrYWxm&urlCount=3 - # lynx --dump 'https://api.fast.com/netflix/speedtest?https=true&token=YXNkZmFzZGxmbnNkYWZoYXNkZmhrYWxm&urlCount=3' | python -mjson.tool - #url = 'https://api.fast.com/netflix/speedtest?https=true&token=YXNkZmFzZGxmbnNkYWZoYXNkZmhrYWxm&urlCount=3' - - - # With the token, get the (3) speed-test-URLS from api.fast.com (which will be in JSON format): - baseurl = 'https://api.fast.com/' - if forceipv4: - # force IPv4 by connecting to an IPv4 address of api.fast.com (over ... HTTP) - ipv4 = findipv4('api.fast.com') - baseurl = 'http://' + ipv4 + '/' # HTTPS does not work IPv4 addresses, thus use HTTP - elif forceipv6: - # force IPv6 - ipv6 = findipv6('api.fast.com') - baseurl = 'http://[' + ipv6 + ']/' - - url = baseurl + 'netflix/speedtest?https=true&token=' + token + '&urlCount=3' # Not more than 3 possible - if verbose: print "API url is", url - try: - urlresult = urllib2.urlopen(url, None, 2) # 2 second time-out - except: - # not good - if verbose: print "No connection possible" # probably IPv6, or just no network - return 0 # no connection, thus no speed - - jsonresult = urlresult.read() - parsedjson = json.loads(jsonresult) - - # Prepare for getting those URLs in a threaded way: - amount = len(parsedjson) - if verbose: print "Number of URLs:", amount - threads = [None] * amount - results = [0] * amount - urls = [None] * amount - i = 0 - for jsonelement in parsedjson: - urls[i] = jsonelement['url'] # fill out speed test url from the json format - if verbose: print jsonelement['url'] - i = i+1 - - # Let's check whether it's IPv6: - for url in urls: - fqdn = url.split('/')[2] - try: - socket.getaddrinfo(fqdn, None, socket.AF_INET6) - if verbose: print "IPv6" - except: - pass - - - # Now start the threads - for i in range(len(threads)): - #print "Thread: i is", i - threads[i] = Thread(target=gethtmlresult, args=(urls[i], results, i)) - threads[i].daemon=True - threads[i].start() - - # Monitor the amount of bytes (and speed) of the threads - time.sleep(1) - sleepseconds = 3 # 3 seconds sleep - lasttotal = 0 - highestspeedkBps = 0 - maxdownload = 60 #MB - nrloops = maxtime / sleepseconds - for loop in range(nrloops): - total = 0 - for i in range(len(threads)): - #print i, results[i] - total += results[i] - delta = total-lasttotal - speedkBps = (delta/sleepseconds)/(1024) - if verbose: - print "Loop", loop, "Total MB", total/(1024*1024), "Delta MB", delta/(1024*1024), "Speed kB/s:", speedkBps, "aka Mbps %.1f" % (application_bytes_to_networkbits(speedkBps)/1024) - ''' - if total/(1024*1024) > maxdownload: - break - ''' - lasttotal = total - if speedkBps > highestspeedkBps: - highestspeedkBps = speedkBps - time.sleep(sleepseconds) - ''' - print "Now wait for threads to end:" - for i in range(len(threads)): - threads[i].join() - ''' - - Mbps = (application_bytes_to_networkbits(highestspeedkBps)/1024) - Mbps = float("%.1f" % Mbps) - if verbose: print "Highest Speed (kB/s):", highestspeedkBps, "aka Mbps ", Mbps - - #print "Debug: total in bytes", total - - return Mbps - - -######## MAIN ################# - - -if __name__ == "__main__": - print "let's speed test:" - print "\nSpeed test, without logging:" - print fast_com() - print "\nSpeed test, with logging:" - print fast_com(verbose=True) - print "\nSpeed test, IPv4, with verbose logging:" - print fast_com(verbose=True, maxtime=18, forceipv4=True) - print "\nSpeed test, IPv6:" - print fast_com(maxtime=12, forceipv6=True) - print "\n30 second speed test:" - fast_com(verbose=True, maxtime=30) - - print "\ndone" - - + ''' + verbose: print debug output + maxtime: max time in seconds to monitor speedtest + forceipv4: force speed test over IPv4 + forceipv6: force speed test over IPv6 + ''' + # go to fast.com to get the javascript file + url = 'https://fast.com/' + try: + urlresult = urllib.urlopen(url) + except: + # no connection at all? + return 0 + response = urlresult.read() + for line in response.split('\n'): + # We're looking for a line like + # + if line.find('script src') >= 0: + jsname = line.split('"')[1] # At time of writing: '/app-40647a.js' + + # From that javascript file, get the token: + url = 'https://fast.com' + jsname + if verbose: + print "javascript url is", url + urlresult = urllib.urlopen(url) + allJSstuff = urlresult.read() + for line in allJSstuff.split(','): + if line.find('token:') >= 0: + if verbose: + print "line is", line + token = line.split('"')[1] + if verbose: + print "token is", token + if token: + break + + baseurl = 'https://api.fast.com/' + if forceipv4: + # force IPv4 by connecting to an IPv4 address of + # api.fast.com (over ... HTTP) + ipv4 = findipv4('api.fast.com') + # HTTPS does not work IPv4 addresses, thus use HTTP + baseurl = 'http://' + ipv4 + '/' + elif forceipv6: + # force IPv6 + ipv6 = findipv6('api.fast.com') + baseurl = 'http://[' + ipv6 + ']/' + + # Not more than 3 possible + url = baseurl + 'netflix/speedtest?https=true&token=' + token + url += '&urlCount=3' + if verbose: + print "API url is", url + try: + urlresult = urllib2.urlopen(url, None, 2) # 2 second time-out + except: + # not good + if verbose: + print "No connection possible" # probably IPv6, or just no network + return 0 # no connection, thus no speed + + jsonresult = urlresult.read() + parsedjson = json.loads(jsonresult) + + # Prepare for getting those URLs in a threaded way: + amount = len(parsedjson) + if verbose: + print "Number of URLs:", amount + threads = [None] * amount + results = [0] * amount + urls = [None] * amount + i = 0 + for jsonelement in parsedjson: + # fill out speed test url from the json format + urls[i] = jsonelement['url'] + if verbose: + print jsonelement['url'] + i = i + 1 + + # Let's check whether it's IPv6: + for url in urls: + fqdn = url.split('/')[2] + try: + socket.getaddrinfo(fqdn, None, socket.AF_INET6) + if verbose: + print "IPv6" + except: + pass + + # Now start the threads + for i in range(len(threads)): + threads[i] = Thread(target=gethtmlresult, args=(urls[i], results, i)) + threads[i].daemon = True + threads[i].start() + + # Monitor the amount of bytes (and speed) of the threads + time.sleep(1) + sleepseconds = 3 # 3 seconds sleep + lasttotal = 0 + highestspeedkBps = 0 + maxdownload = 60 # MB + nrloops = maxtime / sleepseconds + for loop in range(nrloops): + total = 0 + for i in range(len(threads)): + total += results[i] + delta = total-lasttotal + speedkBps = (delta/sleepseconds)/(1024) + if verbose: + total_mb = total / (1024 * 1024) + delta_mb = delta / (1024 * 1024) + net_bits = application_bytes_to_networkbits(speedkBps) / 1024 + mbps = "%.1f" % net_bits + print "Loop", loop, "Total MB", total_mb, "Delta_MB", delta_mb + print "Speed kB/s:", speedkBps, "aka Mbps ", mbps + lasttotal = total + if speedkBps > highestspeedkBps: + highestspeedkBps = speedkBps + time.sleep(sleepseconds) + + Mbps = (application_bytes_to_networkbits(highestspeedkBps)/1024) + Mbps = float("%.1f" % Mbps) + if verbose: + print "Highest Speed (kB/s):", highestspeedkBps, "aka Mbps ", Mbps + + return Mbps + + +if __name__ == "__main__": + print "let's speed test:" + print "\nSpeed test, without logging:" + print fast_com() + print "\nSpeed test, with logging:" + print fast_com(verbose=True) + print "\nSpeed test, IPv4, with verbose logging:" + print fast_com(verbose=True, maxtime=18, forceipv4=True) + print "\nSpeed test, IPv6:" + print fast_com(maxtime=12, forceipv6=True) + print "\n30 second speed test:" + fast_com(verbose=True, maxtime=30) + print "\ndone" diff --git a/fast_com.pyc b/fast_com.pyc index 3b667818fa11e6e181d134257c758fe81681796f..16df17f2eef5d321280142b371defada0264da6f 100644 GIT binary patch delta 1341 zcmah}O>7%Q6n<}Kz1{VCH|zhbDa|IavuQ=@CKwPE0;)zNl_*&0At}&C4&x|s6L0OE zfP&U)qe$EeLL(%^g)5w@D#vo?fP}=YM-GSs7lgPY-mEJwh;+yAXWq~E-t5lvRVxfAVoo4tcr?Dm`OVbtq>C}R083#)nZ#sy-KnLvYftTPFr2!1TL|%p!vaPK40SlXejzp|Fx-#|=2@*Er^ zEZ=$}pIA%yv9z=2?rpH-dEoO<9tDt`1Ad;gX%JONrc^MWqWuhTa?U_yGz)y1T0RRa ze1>E|a+W07R(YM2DKa!jl5v%AN0SOB@!^gR!$2o}i`&w3Dz;ExdbztNI-7FQDLNKyJ9j$4^{u;cAa6T0 z`Kj|GHcr;uPw)d3D4|t&65FA7N0h66=`|- zd*7>I(?EtrWLgGuSYc(Xu`CTeDnSWfj~$_=zN8cmkd@@Qx|%rh6qu?&4eFffBA z*(X>ojdxHj10Bsx_bzi(B|=sB>~&wy$fH6rq!v78dDa<=p7K$S&P=B!a*1k0C88q!11Kp8>Hq)$ delta 1265 zcmZ`&&2Jk;6n}5Ovg@yS9eYGg}aj{18oA)ul_uhQG$1^7- zQ+*kaym(aK_|5zf=js3uSv?50y*9gkb7d~@yj@oQ#wEjEz}k+(S<~yW2CIFxQW0Na zMSO=p8vBxEiSx=wVoO;>OZ=>ebcu}BZSS5_^&4&O^r|g45DR)%e5u;GG&%hg0qCP> z(6EqJ5Z|g_3@1jk>FAUsDw3#(_qEljagrjI)pXmOt*q|9%Nm^b9N%YNlQp}%O=TTv z+rwf`(E--=uSd?Jh!ed?)gNg^@lszJMw%2YqnMba#3s$_H2vGh4~+wi@igQH8uS5r z4@J=|PM3ja;eiSR9X8roKt$>L19=W=%WVLzKwWH_moL6To`+gLd1fv{3xTxsz?OU7 zC@J7&@x*)^*Tr))kMrU`vpQRbAO?36GEEr$vBHa|VK%z1LMaJ1yRNup-NC=auhwGX zPY6b#e-0iX+_-Zh{`KKYs z0Z9~tPe3Lc9)}o%ytMWUu%kf_G?E7eppym}xSNH3P7<)Yrfx`j;SGJjAjre#yaas) zS=vGdc~hTge1;MhDB&!vRGhpuTI=OY^4=d2(ChCzy)DmomRP6fwgbkUt-Thb58K%b z$SdB7E@(^&ZgfVWMMP)OI({1c9uEVJBHZ-6jxXPG-3uDK?9*$Za>LmT8d8apr{0lY zf9G1&U-ILmH+IOi)$v(rexd2hAfb>UVcBhMJAS}UDP_s2Ecw>|Nw-{pBoK(-?C7{M-&1~163SDRnakqSv8{+wFos%jM^ub$YLgx$3iqPYEf$` zF|-8OOY$6_gorr^q2YR*nd5F|a^^ zBa((!b%V;X&>l%*l89+dyk|RjS^Q!zMCYh{SN8ncy!hK@SQW4AkMkF#VL}2D1S)}l z;ICE0X6gzqiyu;@VYgDe^uaxk@9jEQ4M|!gFz7I6Ly0L#)k;_+mT6g;pM*V@L*+FT C5B`q; diff --git a/fast_com_example_usage.py b/fast_com_example_usage.py index e040943..6b68776 100755 --- a/fast_com_example_usage.py +++ b/fast_com_example_usage.py @@ -1,8 +1,7 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 import fast_com -print "Start speedtest against fast.com ..." -print "Result:", fast_com.fast_com(), "Mbps" -print "... Done" - +print("Start speedtest against fast.com ...") +print("Result:", fast_com.fast_com(), "Mbps") +print("... Done") diff --git a/sab_proto.py b/sab_proto.py index 5f7d23a..837cdf2 100755 --- a/sab_proto.py +++ b/sab_proto.py @@ -7,8 +7,7 @@ print "Speed test [Mbps]:", print fast_com(maxtime=7) -print "Speed test, IPv4 [Mbps]:", +print "Speed test, IPv4 [Mbps]:", print fast_com(maxtime=7, forceipv4=True) -print "Speed test, IPv6 [Mbps]:", +print "Speed test, IPv6 [Mbps]:", print fast_com(maxtime=7, forceipv6=True) - From c222e6a1031a17e91db27ca53561cd54cb9c0f6b Mon Sep 17 00:00:00 2001 From: Justin Austin Date: Sat, 22 Jul 2017 21:01:19 -0400 Subject: [PATCH 2/5] switched to python3 --- fast_com.py | 51 ++++++++++++++++++++++++--------------------------- fast_com.pyc | Bin 4905 -> 4810 bytes sab_proto.py | 17 ++++++++--------- 3 files changed, 32 insertions(+), 36 deletions(-) diff --git a/fast_com.py b/fast_com.py index c871f53..8b1939f 100644 --- a/fast_com.py +++ b/fast_com.py @@ -5,11 +5,10 @@ ''' -import os import json import urllib import urllib2 -import sys +import socket import time from threading import Thread @@ -41,7 +40,6 @@ def findipv4(fqdn): ''' find IPv4 address of fqdn ''' - import socket ipv4 = socket.getaddrinfo(fqdn, 80, socket.AF_INET)[0][4][0] return ipv4 @@ -50,7 +48,6 @@ def findipv6(fqdn): ''' find IPv6 address of fqdn ''' - import socket ipv6 = socket.getaddrinfo(fqdn, 80, socket.AF_INET6)[0][4][0] return ipv6 @@ -79,16 +76,16 @@ def fast_com(verbose=False, maxtime=15, forceipv4=False, forceipv6=False): # From that javascript file, get the token: url = 'https://fast.com' + jsname if verbose: - print "javascript url is", url + print("javascript url is", url) urlresult = urllib.urlopen(url) allJSstuff = urlresult.read() for line in allJSstuff.split(','): if line.find('token:') >= 0: if verbose: - print "line is", line + print("line is", line) token = line.split('"')[1] if verbose: - print "token is", token + print("token is", token) if token: break @@ -108,13 +105,14 @@ def fast_com(verbose=False, maxtime=15, forceipv4=False, forceipv6=False): url = baseurl + 'netflix/speedtest?https=true&token=' + token url += '&urlCount=3' if verbose: - print "API url is", url + print("API url is", url) try: urlresult = urllib2.urlopen(url, None, 2) # 2 second time-out except: # not good if verbose: - print "No connection possible" # probably IPv6, or just no network + # probably IPv6, or just no network + print("No connection possible") return 0 # no connection, thus no speed jsonresult = urlresult.read() @@ -123,7 +121,7 @@ def fast_com(verbose=False, maxtime=15, forceipv4=False, forceipv6=False): # Prepare for getting those URLs in a threaded way: amount = len(parsedjson) if verbose: - print "Number of URLs:", amount + print("Number of URLs:", amount) threads = [None] * amount results = [0] * amount urls = [None] * amount @@ -132,7 +130,7 @@ def fast_com(verbose=False, maxtime=15, forceipv4=False, forceipv6=False): # fill out speed test url from the json format urls[i] = jsonelement['url'] if verbose: - print jsonelement['url'] + print(jsonelement['url']) i = i + 1 # Let's check whether it's IPv6: @@ -141,7 +139,7 @@ def fast_com(verbose=False, maxtime=15, forceipv4=False, forceipv6=False): try: socket.getaddrinfo(fqdn, None, socket.AF_INET6) if verbose: - print "IPv6" + print("IPv6") except: pass @@ -156,7 +154,6 @@ def fast_com(verbose=False, maxtime=15, forceipv4=False, forceipv6=False): sleepseconds = 3 # 3 seconds sleep lasttotal = 0 highestspeedkBps = 0 - maxdownload = 60 # MB nrloops = maxtime / sleepseconds for loop in range(nrloops): total = 0 @@ -169,8 +166,8 @@ def fast_com(verbose=False, maxtime=15, forceipv4=False, forceipv6=False): delta_mb = delta / (1024 * 1024) net_bits = application_bytes_to_networkbits(speedkBps) / 1024 mbps = "%.1f" % net_bits - print "Loop", loop, "Total MB", total_mb, "Delta_MB", delta_mb - print "Speed kB/s:", speedkBps, "aka Mbps ", mbps + print("Loop", loop, "Total MB", total_mb, "Delta_MB", delta_mb) + print("Speed kB/s:", speedkBps, "aka Mbps ", mbps) lasttotal = total if speedkBps > highestspeedkBps: highestspeedkBps = speedkBps @@ -179,21 +176,21 @@ def fast_com(verbose=False, maxtime=15, forceipv4=False, forceipv6=False): Mbps = (application_bytes_to_networkbits(highestspeedkBps)/1024) Mbps = float("%.1f" % Mbps) if verbose: - print "Highest Speed (kB/s):", highestspeedkBps, "aka Mbps ", Mbps + print("Highest Speed (kB/s):", highestspeedkBps, "aka Mbps ", Mbps) return Mbps if __name__ == "__main__": - print "let's speed test:" - print "\nSpeed test, without logging:" - print fast_com() - print "\nSpeed test, with logging:" - print fast_com(verbose=True) - print "\nSpeed test, IPv4, with verbose logging:" - print fast_com(verbose=True, maxtime=18, forceipv4=True) - print "\nSpeed test, IPv6:" - print fast_com(maxtime=12, forceipv6=True) - print "\n30 second speed test:" + print("let's speed test:") + print("\nSpeed test, without logging:") + print(fast_com()) + print("\nSpeed test, with logging:") + print(fast_com(verbose=True)) + print("\nSpeed test, IPv4, with verbose logging:") + print(fast_com(verbose=True, maxtime=18, forceipv4=True)) + print("\nSpeed test, IPv6:") + print(fast_com(maxtime=12, forceipv6=True)) + print("\n30 second speed test:") fast_com(verbose=True, maxtime=30) - print "\ndone" + print("\ndone") diff --git a/fast_com.pyc b/fast_com.pyc index 16df17f2eef5d321280142b371defada0264da6f..f7411810c1cd5ceec23db0fd65cbdeadf9552191 100644 GIT binary patch delta 1324 zcmbtUxo;e06#u>XX6M@bKAbpvd2JL+iR4HY2||%T9160cMPiBN$i;qZ6epOhJwgf0 z$b|zX2z@nw01`^GLI?yYC~4@@P?&<23JOZzn=v6hJM-SWYv1o3-^^E|pI4UDpZVbQ zucKR2oc&yWUS~RLeX;cBvBEtE@4MJ`LE6S6VB3SvB!Um^Btih~CPEgo9JD7~1RsxV zZ09j6KnJ*M;jl(Ji`U=KIcBpwP9RD?RdfNfBAbX!#WT}OiA|TF%g~k7uh?Hb^RuG{ zT~8erI~vdBT3n>-7M09B_0x^6bYwAuJtgKM+Dxx79j!47fqg;*Xi8)YCkom^TPr4h z#_T1$y=I$F>~-_4eXSumLP@eW*9VDk=l+LROjlhsJ5F}%%BtiRKkOm>Z`8+`b@N;1 zq9dr|ifK6Gl}pb9+$pj)6}Taj$CUIIn$Mk210!3BZ0uR)yxT8_hdWN7!c#n5b zLM`UQU_DbR&?EEMyG+`gdM$d{{Ow&@9U;tNzaY7jl;2spllC*2KeQN!`MdLnX3M`# zznZW8-Z3vXYN8%@@ZfiZMMRx+Hct^R30!;*57xvHa#v;XC8Mh$t|2;ysDh}5_9lnT zKUNd5w9tyrBjTI$>SfNooQ!Tqu~)CMenGr?jiv4}!X~2SWXclSgJy3JjkpipMZ`7q z5M6-oBkE$$;b!ze{-B45`q;DCz-eP18Kbmu*T!MrW;t$yw;0y3-;hGMXqxXRz9$0M z{ugi~9w2NbpaV2Gignq=eOs?cn7|BD7&#OJeiL3;IN58~;@V)X{@3)XK*-& z$M&MiNxXSl+Gq!y+hK2oBMU4Ae3h7*i(%QOVTmgUY>Xl&@--4(+si(r zIA}&LY*8XbS4fm}Kn0SeG`K=YNa@lcx->|Xh>9wFcQ%Rq1DKh6&Ueo_bDnqR@!}5` z8tR{X=BvMlA8+CL<&)p!8nzxbZXG(<^|0YV3$((nkG%jJ0Ytkv1~$^rsRU%8y#!>T z{RHH&orexAwIGdS7aIj^7ojugIvC7}&SIsbb0pgZ9Kmk#I>i!)%jwt41P z$Gpg-QWHAzknHOg()^o|uF5{E|M0xNhekO`{q&aPTKaMMRBpLw-j*W1A4q znxE5+!&yWXM3V@Y<5~3YtF?%d8fx)5M11H$H|eFBv};{atCzT+vs(QM(L6@eh?+@j zt>pqVl?#}P&qL3%<{7N$1@bxa^W>Cubc>)#!OS87r5!qsYt~N`qU-X5bEBr|WvcJ4 ztNyxLvoXS#F99_+V)MEicP^=5l3w<_$`i5L10| PmsGlx_S3w~PnG(AJb&;8 diff --git a/sab_proto.py b/sab_proto.py index 837cdf2..a1d2991 100755 --- a/sab_proto.py +++ b/sab_proto.py @@ -2,12 +2,11 @@ from fast_com import fast_com -print "Starting Speed test against fast.com" - - -print "Speed test [Mbps]:", -print fast_com(maxtime=7) -print "Speed test, IPv4 [Mbps]:", -print fast_com(maxtime=7, forceipv4=True) -print "Speed test, IPv6 [Mbps]:", -print fast_com(maxtime=7, forceipv6=True) +print("Starting Speed test against fast.com") + +print("Speed test [Mbps]:") +print(fast_com(maxtime=7)) +print("Speed test, IPv4 [Mbps]:") +print(fast_com(maxtime=7, forceipv4=True)) +print("Speed test, IPv6 [Mbps]:") +print(fast_com(maxtime=7, forceipv6=True)) From 5b23f4ee95cc103d9f328424523255667e295cc6 Mon Sep 17 00:00:00 2001 From: Justin Austin Date: Sat, 22 Jul 2017 23:05:19 -0400 Subject: [PATCH 3/5] switch to requests from urllib(2) --- fast_com.py | 44 +++++++++++++++++++++----------------------- 1 file changed, 21 insertions(+), 23 deletions(-) diff --git a/fast_com.py b/fast_com.py index 8b1939f..3d31d30 100644 --- a/fast_com.py +++ b/fast_com.py @@ -6,8 +6,7 @@ import json -import urllib -import urllib2 +import requests import socket import time from threading import Thread @@ -18,15 +17,11 @@ def gethtmlresult(url, result, index): get the stuff from url in chuncks of size CHUNK, and keep writing the number of bytes retrieved into result[index] ''' - req = urllib2.urlopen(url) - CHUNK = 100 * 1024 - i = 1 - while True: - chunk = req.read(CHUNK) - if not chunk: - break - result[index] = i * CHUNK - i = i + 1 + req = requests.get(url, stream=True) + CHUNK_SIZE = 100 * 1024 + for i, chunk in enumerate(req.iter_content(CHUNK_SIZE)): + result[index] = i * CHUNK_SIZE + i += 1 def application_bytes_to_networkbits(bytes): @@ -62,11 +57,12 @@ def fast_com(verbose=False, maxtime=15, forceipv4=False, forceipv6=False): # go to fast.com to get the javascript file url = 'https://fast.com/' try: - urlresult = urllib.urlopen(url) - except: + urlresult = requests.get(url) + except Exception as e: + print(e) # no connection at all? return 0 - response = urlresult.read() + response = urlresult.text for line in response.split('\n'): # We're looking for a line like # @@ -77,8 +73,8 @@ def fast_com(verbose=False, maxtime=15, forceipv4=False, forceipv6=False): url = 'https://fast.com' + jsname if verbose: print("javascript url is", url) - urlresult = urllib.urlopen(url) - allJSstuff = urlresult.read() + urlresult = requests.get(url) + allJSstuff = urlresult.text for line in allJSstuff.split(','): if line.find('token:') >= 0: if verbose: @@ -107,15 +103,16 @@ def fast_com(verbose=False, maxtime=15, forceipv4=False, forceipv6=False): if verbose: print("API url is", url) try: - urlresult = urllib2.urlopen(url, None, 2) # 2 second time-out - except: + urlresult = requests.post(url, data=None, timeout=2) + except Exception as e: + print(e) # not good if verbose: # probably IPv6, or just no network print("No connection possible") return 0 # no connection, thus no speed - jsonresult = urlresult.read() + jsonresult = urlresult.text parsedjson = json.loads(jsonresult) # Prepare for getting those URLs in a threaded way: @@ -154,7 +151,7 @@ def fast_com(verbose=False, maxtime=15, forceipv4=False, forceipv6=False): sleepseconds = 3 # 3 seconds sleep lasttotal = 0 highestspeedkBps = 0 - nrloops = maxtime / sleepseconds + nrloops = maxtime // sleepseconds for loop in range(nrloops): total = 0 for i in range(len(threads)): @@ -169,9 +166,10 @@ def fast_com(verbose=False, maxtime=15, forceipv4=False, forceipv6=False): print("Loop", loop, "Total MB", total_mb, "Delta_MB", delta_mb) print("Speed kB/s:", speedkBps, "aka Mbps ", mbps) lasttotal = total - if speedkBps > highestspeedkBps: - highestspeedkBps = speedkBps - time.sleep(sleepseconds) + + if speedkBps > highestspeedkBps: + highestspeedkBps = speedkBps + time.sleep(sleepseconds) Mbps = (application_bytes_to_networkbits(highestspeedkBps)/1024) Mbps = float("%.1f" % Mbps) From 9b26d9314485cc972a6d98198b17383736c1645b Mon Sep 17 00:00:00 2001 From: Justin Austin Date: Sat, 22 Jul 2017 23:09:02 -0400 Subject: [PATCH 4/5] added requirements.txt --- requirements.txt | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 requirements.txt diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..c036bb6 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,5 @@ +certifi==2017.4.17 +chardet==3.0.4 +idna==2.5 +requests==2.18.1 +urllib3==1.21.1 From 1e733f1467298a2aeb9390172393103e4d20003e Mon Sep 17 00:00:00 2001 From: Justin Austin Date: Sat, 22 Jul 2017 23:18:51 -0400 Subject: [PATCH 5/5] changed README to include python3 examples --- README.md | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 8f5dded..941aa16 100644 --- a/README.md +++ b/README.md @@ -4,26 +4,25 @@ Python CLI-tool (without need for a GUI) to measure Internet speed with fast.com Example usage ``` -$ python fast_com_example_usage.py +$ python3 fast_com_example_usage.py Start speedtest against fast.com ... Result: 53.4 Mbps ... Done ``` ``` -$ python -Python 2.7.6 (default, Jun 22 2015, 18:00:18) -[GCC 4.8.2] on linux2 +$ python3 +Python 3.6.1 (default, Jun 27 2017, 14:35:15) +[GCC 7.1.1 20170622 (Red Hat 7.1.1-3)] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import fast_com ->>> print fast_com.fast_com(maxtime=6) -55.6 - +>>> print(fast_com.fast_com(maxtime=6)) +32.5 ``` Full verbose: ``` -$ python fast_com.py +$ python3 fast_com.py let's go: javascript url is https://fast.com/app-ab2f99.js token is YXNkZmFzZGxmbnNkYWZoYXNkZmhrYWxm @@ -41,8 +40,4 @@ Loop 6 Total MB 58 Delta MB 3 Speed kB/s: 1100 aka Mbps 9.0 Loop 7 Total MB 60 Delta MB 1 Speed kB/s: 500 aka Mbps 4.1 Highest Speed (kB/s): 6533 aka Mbps 53.2 done - ``` - - -