Gather information about configuration from multiple URLs

Patch add functionality for gathering informatio about
configuration and server list from multiple URLs.
URLs prefix is defined via global variables.

Author Dubravko Penezic
This commit is contained in:
Dubravko Penezic 2015-03-23 22:43:23 +01:00
parent 72da41e4fc
commit 0fbfdf7ea9

View File

@ -34,6 +34,13 @@ shutdown_event = None
# Used for bound_interface # Used for bound_interface
socket_socket = socket.socket socket_socket = socket.socket
# Config server URL prefix
conf_server_urls_prefix = [
'http://www.speedtest.net/',
'https://www.speedtest.net/',
'http://c.speedtest.net/',
]
try: try:
import xml.etree.cElementTree as ET import xml.etree.cElementTree as ET
except ImportError: except ImportError:
@ -145,6 +152,12 @@ class SpeedtestCliServerListError(Exception):
""" """
class SpeedtestConfigDataError(Exception):
"""Internal Exception class used to indicate to move on to the next
URL for retrieving speedtest.net configuration details
"""
def bound_socket(*args, **kwargs): def bound_socket(*args, **kwargs):
"""Bind socket to a specified source IP address""" """Bind socket to a specified source IP address"""
@ -340,18 +353,22 @@ def getConfig():
we are interested in we are interested in
""" """
request = build_request('https://www.speedtest.net/speedtest-config.php') config = {}
for url_prefix in conf_server_urls_prefix:
url = url_prefix+'speedtest-config.php'
try:
request = build_request(url)
uh = catch_request(request) uh = catch_request(request)
if uh is False: if uh is False:
print_('Could not retrieve speedtest.net configuration') raise SpeedtestConfigDataError
sys.exit(1)
configxml = [] configxml = []
while 1: while 1:
configxml.append(uh.read(10240)) configxml.append(uh.read(10240))
if len(configxml[-1]) == 0: if len(configxml[-1]) == 0:
break break
if int(uh.code) != 200: if int(uh.code) != 200:
return None uh.close
raise SpeedtestConfigDataError
uh.close() uh.close()
try: try:
try: try:
@ -369,10 +386,21 @@ def getConfig():
'download': getAttributesByTagName(root, 'download'), 'download': getAttributesByTagName(root, 'download'),
'upload': getAttributesByTagName(root, 'upload')} 'upload': getAttributesByTagName(root, 'upload')}
except SyntaxError: except SyntaxError:
print_('Failed to parse speedtest.net configuration') raise SpeedtestConfigDataError
sys.exit(1)
del root del root
del configxml del configxml
except SpeedtestConfigDataError:
continue
# We were able to fetch and parse the list of speedtest.net servers
if config:
break
if not config:
print_('Could not retrieve speedtest.net configuration')
sys.exit(1)
return config return config
@ -381,12 +409,9 @@ def closestServers(client, all=False):
distance distance
""" """
urls = [
'https://www.speedtest.net/speedtest-servers-static.php',
'http://c.speedtest.net/speedtest-servers-static.php',
]
servers = {} servers = {}
for url in urls: for url_prefix in conf_server_urls_prefix:
url = url_prefix+'speedtest-servers-static.php'
try: try:
request = build_request(url) request = build_request(url)
uh = catch_request(request) uh = catch_request(request)