From 6d9991287826bc5c9d610c9852a2b64af6bb88ce Mon Sep 17 00:00:00 2001 From: Benjamin Piouffle Date: Mon, 17 Oct 2016 09:55:27 +1100 Subject: [PATCH] Implement case insensitive country filter with --country option and update README accordingly --- README.rst | 1 + speedtest_cli.py | 14 +++++++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/README.rst b/README.rst index fccf934..ad468a2 100644 --- a/README.rst +++ b/README.rst @@ -92,6 +92,7 @@ Usage --list Display a list of speedtest.net servers sorted by distance --server SERVER Specify a server ID to test against + --country COUNTRY Select the best server matching country name --mini MINI URL of the Speedtest Mini server --source SOURCE Source IP address to bind to --timeout TIMEOUT HTTP timeout in seconds. Default 10 diff --git a/speedtest_cli.py b/speedtest_cli.py index d93d5c3..2e701ed 100755 --- a/speedtest_cli.py +++ b/speedtest_cli.py @@ -583,6 +583,8 @@ def speedtest(): help='Display a list of speedtest.net servers ' 'sorted by distance') parser.add_argument('--server', help='Specify a server ID to test against') + parser.add_argument('--country', type=str.lower, + help='Select the best server matching country name') 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('--timeout', default=10, type=int, @@ -627,7 +629,7 @@ def speedtest(): if not args.simple: print_('Retrieving speedtest.net server list...') - if args.list or args.server: + if args.list or args.server or args.country: servers = closestServers(config['client'], True) if args.list: serverList = [] @@ -650,6 +652,16 @@ def speedtest(): except IndexError: print_('Invalid server ID') sys.exit(1) + elif args.country: + try: + if not args.simple: + print_('Selecting best server in %s based on latency...' % + args.country.capitalize()) + best = getBestServer(filter(lambda x: x['country'].lower() == + args.country, servers)) + except IndexError: + print_('No server available for this country') + sys.exit(1) elif args.mini: name, ext = os.path.splitext(args.mini) if ext: