Bug fixes, etc.

If multiple servers reported the exact same lat. and long. then they
would overwrite eachother in the dict.  I changed it to use a
defaultdict so that you could have multiple servers with the exact same
distance. (3 of my closest 5 reported the exact same corrdinates.)

The latency it was printing was not the latency from the chosen best
server.  Now it is.

Added a print out of the IP and ISP info.
This commit is contained in:
Andrew Parker 2013-01-22 16:52:46 -07:00
parent a2f2a46f6f
commit c0f0087fd6
1 changed files with 17 additions and 6 deletions

View File

@ -23,6 +23,7 @@ import sys
import threading
from Queue import Queue
from xml.dom import minidom as DOM
from collections import defaultdict
try:
from urlparse import parse_qs
except ImportError:
@ -197,16 +198,22 @@ def closestServers(client):
return None
uh.close()
root = DOM.parseString(serversxml)
servers = {}
servers = defaultdict(list)
for server in root.getElementsByTagName('server'):
attrib = dict(server.attributes.items())
d = distance([float(client['lat']), float(client['lon'])],
[float(attrib.get('lat')), float(attrib.get('lon'))])
servers[d] = attrib
servers[d].append(attrib)
closest = []
for d in sorted(servers.keys())[0:4]:
closest.append(servers[d])
for d in sorted(servers.keys()):
for s in servers[d]:
closest.append(s)
if(len(closest) == 5):
break
else:
continue
break
del servers
del root
@ -235,8 +242,9 @@ def getBestServer(servers):
avg = round((cum / 3) * 1000000, 3)
results[avg] = server
best = results[sorted(results.keys())[0]]
best['latency'] = avg
fastest = sorted(results.keys())[0]
best = results[fastest]
best['latency'] = fastest
return best
@ -247,6 +255,9 @@ def speedtest():
print 'Retrieving speedtest.net configuration...'
config = getConfig()
client = dict(config['client'].items())
print 'Testing IP: %(ip)s from ISP: %(isp)s' % client
print 'Retrieving speedtest.net server list...'
servers = closestServers(config['client'])