Allow specifying an alternate server URL

This fixes #119; I can now run

    python speedtest-cli.py --list-url https://phihag.de/speedtest-cli/serverlist.php

even from a CHINANET Internet connection.
This commit is contained in:
Philipp Hagemeister 2015-01-07 15:08:13 +01:00
parent 795bc51da4
commit d9fc21c416
1 changed files with 12 additions and 6 deletions

View File

@ -337,19 +337,20 @@ def getConfig():
return config
def closestServers(client, all=False):
def closestServers(client, list_url, all=False):
"""Determine the 5 closest speedtest.net servers based on geographic
distance
"""
uh = urlopen('http://www.speedtest.net/speedtest-servers-static.php')
uh = urlopen(list_url)
serversxml = []
while 1:
serversxml.append(uh.read(10240))
if len(serversxml[-1]) == 0:
break
if int(uh.code) != 200:
return None
if getattr(uh, 'code', None) is not None: # Not a file: URL
if int(uh.code) != 200:
return None
uh.close()
try:
try:
@ -484,6 +485,11 @@ def speedtest():
parser.add_argument('--server', help='Specify a server ID to test against')
parser.add_argument('--mini', help='URL of the Speedtest Mini server')
parser.add_argument('--source', help='Source IP address to bind to')
parser.add_argument('--list-url', metavar='URL',
help='URL to load the server list from. '
'Start with file: to load from a local file.',
default='http://www.speedtest.net'
'/speedtest-servers-static.php')
parser.add_argument('--version', action='store_true',
help='Show the version number and exit')
@ -514,7 +520,7 @@ def speedtest():
if not args.simple:
print_('Retrieving speedtest.net server list...')
if args.list or args.server:
servers = closestServers(config['client'], True)
servers = closestServers(config['client'], args.list_url, True)
if args.list:
serverList = []
for server in servers:
@ -533,7 +539,7 @@ def speedtest():
pass
sys.exit(0)
else:
servers = closestServers(config['client'])
servers = closestServers(config['client'], args.list_url)
if not args.simple:
print_('Testing from %(isp)s (%(ip)s)...' % config['client'])