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