Switch to httplib/http.client to allow for better timing of latency, results may be slightly higher than previous, but more reliable

This commit is contained in:
Matt Martz 2014-05-27 09:30:39 -05:00
parent b14e104ad1
commit c1b9a0db0a
1 changed files with 23 additions and 13 deletions

View File

@ -48,6 +48,11 @@ try:
except ImportError: except ImportError:
from urllib.request import urlopen, Request, HTTPError, URLError from urllib.request import urlopen, Request, HTTPError, URLError
try:
from httplib import HTTPConnection, HTTPSConnection
except ImportError:
from http.client import HTTPConnection, HTTPSConnection
try: try:
from Queue import Queue from Queue import Queue
except ImportError: except ImportError:
@ -380,31 +385,36 @@ def closestServers(client, all=False):
def getBestServer(servers): def getBestServer(servers):
"""Perform a speedtest.net "ping" to determine which speedtest.net """Perform a speedtest.net latency request to determine which
server has the lowest latency speedtest.net server has the lowest latency
""" """
results = {} results = {}
for server in servers: for server in servers:
cum = [] cum = []
url = os.path.dirname(server['url']) url = '%s/latency.txt' % os.path.dirname(server['url'])
urlparts = urlparse(url)
for i in range(0, 3): for i in range(0, 3):
try: try:
uh = urlopen('%s/latency.txt' % url) if urlparts[0] == 'https':
h = HTTPSConnection(urlparts[1])
else:
h = HTTPConnection(urlparts[1])
start = time.time()
h.request("GET", urlparts[2])
r = h.getresponse()
total = (time.time() - start)
except (HTTPError, URLError): except (HTTPError, URLError):
cum.append(3600) cum.append(3600)
continue continue
start = time.time() text = r.read(9)
text = uh.read(9) if int(r.status) == 200 and text == 'test=test'.encode():
total = time.time() - start
if int(uh.code) == 200 and text == 'test=test'.encode():
cum.append(total) cum.append(total)
else: else:
cum.append(3600) cum.append(3600)
uh.close() h.close()
avg = round((sum(cum) / 3) * 1000000, 3) avg = round((sum(cum) / 6) * 1000, 3)
results[avg] = server results[avg] = server
fastest = sorted(results.keys())[0] fastest = sorted(results.keys())[0]
best = results[fastest] best = results[fastest]
best['latency'] = fastest best['latency'] = fastest
@ -573,7 +583,7 @@ def speedtest():
best = servers[0] best = servers[0]
else: else:
if not args.simple: if not args.simple:
print_('Selecting best server based on ping...') print_('Selecting best server based on latency...')
best = getBestServer(servers) best = getBestServer(servers)
if not args.simple: if not args.simple:
@ -588,7 +598,7 @@ def speedtest():
print_('Hosted by %(sponsor)s (%(name)s) [%(d)0.2f km]: ' print_('Hosted by %(sponsor)s (%(name)s) [%(d)0.2f km]: '
'%(latency)s ms' % best) '%(latency)s ms' % best)
else: else:
print_('Ping: %(latency)s ms' % best) print_('Latency: %(latency)s ms' % best)
sizes = [350, 500, 750, 1000, 1500, 2000, 2500, 3000, 3500, 4000] sizes = [350, 500, 750, 1000, 1500, 2000, 2500, 3000, 3500, 4000]
urls = [] urls = []