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 committed by Matt Martz
parent 824ec51280
commit 357649cba7
1 changed files with 14 additions and 6 deletions

View File

@ -23,6 +23,7 @@ import sys
import threading import threading
from Queue import Queue from Queue import Queue
from xml.dom import minidom as DOM from xml.dom import minidom as DOM
from collections import defaultdict
try: try:
from urlparse import parse_qs from urlparse import parse_qs
except ImportError: except ImportError:
@ -203,16 +204,22 @@ def closestServers(client):
return None return None
uh.close() uh.close()
root = DOM.parseString(serversxml) root = DOM.parseString(serversxml)
servers = {} servers = defaultdict(list)
for server in root.getElementsByTagName('server'): for server in root.getElementsByTagName('server'):
attrib = dict(server.attributes.items()) attrib = dict(server.attributes.items())
d = distance([float(client['lat']), float(client['lon'])], d = distance([float(client['lat']), float(client['lon'])],
[float(attrib.get('lat')), float(attrib.get('lon'))]) [float(attrib.get('lat')), float(attrib.get('lon'))])
servers[d] = attrib servers[d].append(attrib)
closest = [] closest = []
for d in sorted(servers.keys())[0:4]: for d in sorted(servers.keys()):
closest.append(servers[d]) for s in servers[d]:
closest.append(s)
if(len(closest) == 5):
break
else:
continue
break
del servers del servers
del root del root
@ -241,8 +248,9 @@ def getBestServer(servers):
avg = round((cum / 3) * 1000000, 3) avg = round((cum / 3) * 1000000, 3)
results[avg] = server results[avg] = server
best = results[sorted(results.keys())[0]] fastest = sorted(results.keys())[0]
best['latency'] = avg best = results[fastest]
best['latency'] = fastest
return best return best