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:
parent
72da41e4fc
commit
0fbfdf7ea9
|
@ -34,6 +34,13 @@ shutdown_event = None
|
|||
# Used for bound_interface
|
||||
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:
|
||||
import xml.etree.cElementTree as ET
|
||||
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):
|
||||
"""Bind socket to a specified source IP address"""
|
||||
|
@ -340,18 +353,22 @@ def getConfig():
|
|||
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)
|
||||
if uh is False:
|
||||
print_('Could not retrieve speedtest.net configuration')
|
||||
sys.exit(1)
|
||||
raise SpeedtestConfigDataError
|
||||
configxml = []
|
||||
while 1:
|
||||
configxml.append(uh.read(10240))
|
||||
if len(configxml[-1]) == 0:
|
||||
break
|
||||
if int(uh.code) != 200:
|
||||
return None
|
||||
uh.close
|
||||
raise SpeedtestConfigDataError
|
||||
uh.close()
|
||||
try:
|
||||
try:
|
||||
|
@ -369,10 +386,21 @@ def getConfig():
|
|||
'download': getAttributesByTagName(root, 'download'),
|
||||
'upload': getAttributesByTagName(root, 'upload')}
|
||||
except SyntaxError:
|
||||
print_('Failed to parse speedtest.net configuration')
|
||||
sys.exit(1)
|
||||
raise SpeedtestConfigDataError
|
||||
del root
|
||||
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
|
||||
|
||||
|
||||
|
@ -381,12 +409,9 @@ def closestServers(client, all=False):
|
|||
distance
|
||||
"""
|
||||
|
||||
urls = [
|
||||
'https://www.speedtest.net/speedtest-servers-static.php',
|
||||
'http://c.speedtest.net/speedtest-servers-static.php',
|
||||
]
|
||||
servers = {}
|
||||
for url in urls:
|
||||
for url_prefix in conf_server_urls_prefix:
|
||||
url = url_prefix+'speedtest-servers-static.php'
|
||||
try:
|
||||
request = build_request(url)
|
||||
uh = catch_request(request)
|
||||
|
|
Loading…
Reference in New Issue