From 65a56097e522e4886962f6f08e1dfc96bc12519d Mon Sep 17 00:00:00 2001 From: Sam Stoelinga Date: Mon, 12 Sep 2016 23:07:38 -0700 Subject: [PATCH] Add support to write results to json file --- speedtest_cli.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/speedtest_cli.py b/speedtest_cli.py index d93d5c3..152fe36 100755 --- a/speedtest_cli.py +++ b/speedtest_cli.py @@ -15,6 +15,7 @@ # License for the specific language governing permissions and limitations # under the License. +import json import os import re import sys @@ -579,6 +580,8 @@ def speedtest(): parser.add_argument('--simple', action='store_true', help='Suppress verbose output, only show basic ' 'information') + parser.add_argument('--json', + help='Write results to specified JSON file') parser.add_argument('--list', action='store_true', help='Display a list of speedtest.net servers ' 'sorted by distance') @@ -735,6 +738,13 @@ def speedtest(): print_('Upload: %0.2f M%s/s' % ((ulspeed / 1000 / 1000) * args.units[1], args.units[0])) + if args.json: + results = {"download": (dlspeed / 1000 / 1000) * args.units[1], + "upload": (ulspeed / 1000 / 1000) * args.units[1]} + with open(args.json, 'w') as jsonfile: + json.dump(results, jsonfile) + print_("Wrote results to JSON file: %s" % args.json) + if args.share and args.mini: print_('Cannot generate a speedtest.net share results image while ' 'testing against a Speedtest Mini server')