First pass at adding some command line options, the default is not to automatically generate the share link

This commit is contained in:
Matt Martz 2013-01-24 14:40:29 -06:00
parent a2f2a46f6f
commit 54505e6edd
2 changed files with 173 additions and 95 deletions

View File

@ -31,6 +31,10 @@ try:
from hashlib import md5
except ImportError:
from md5 import md5
try:
from argparse import ArgumentParser as ArgParser
except ImportError:
from optparse import OptionParser as ArgParser
def distance(origin, destination):
@ -74,7 +78,7 @@ class FileGetter(threading.Thread):
self.result = ''
def downloadSpeed(files):
def downloadSpeed(files, quiet=False):
start = time.time()
def producer(q, files):
@ -82,6 +86,7 @@ def downloadSpeed(files):
thread = FileGetter(file, start)
thread.start()
q.put(thread, True)
if not quiet:
sys.stdout.write('.')
sys.stdout.flush()
@ -130,7 +135,7 @@ class FilePutter(threading.Thread):
self.result = ''
def uploadSpeed(url, sizes):
def uploadSpeed(url, sizes, quiet=False):
start = time.time()
def producer(q, sizes):
@ -138,6 +143,7 @@ def uploadSpeed(url, sizes):
thread = FilePutter(url, start, size)
thread.start()
q.put(thread, True)
if not quiet:
sys.stdout.write('.')
sys.stdout.flush()
@ -244,15 +250,47 @@ def getBestServer(servers):
def speedtest():
"""Run the full speedtest.net test"""
description = (
'Command line interface for testing internet bandwidth using '
'speedtest.net.\n'
'------------------------------------------------------------'
'--------------\n'
'https://github.com/sivel/speedtest-cli')
parser = ArgParser(description=description)
try:
parser.add_argument = parser.add_option
except AttributeError:
pass
parser.add_argument('--share', action='store_true',
help='Generate and provide a URL to the speedtest.net '
'share results image')
parser.add_argument('--simple', action='store_true',
help='Suppress verbose output, only show basic '
'information')
options = parser.parse_args()
if isinstance(options, tuple):
args = options[0]
else:
args = options
del options
if not args.simple:
print 'Retrieving speedtest.net configuration...'
config = getConfig()
if not args.simple:
print 'Retrieving speedtest.net server list...'
servers = closestServers(config['client'])
if not args.simple:
print 'Selecting best server based on ping...'
best = getBestServer(servers)
if not args.simple:
print 'Hosted by %(sponsor)s (%(name)s): %(latency)sms' % best
else:
print 'Ping: %(latency)sms' % best
sizes = [350, 500, 750, 1000, 1500, 2000, 2500, 3000, 3500, 4000]
urls = []
@ -260,19 +298,26 @@ def speedtest():
for i in xrange(0, 4):
urls.append('%s/random%sx%s.jpg' %
(os.path.dirname(best['url']), size, size))
if not args.simple:
print 'Testing download speed',
dlspeed = downloadSpeed(urls)
print '\nDownload: %s Mbit/s' % round((dlspeed / 1024 / 1024) * 8, 2)
dlspeed = downloadSpeed(urls, args.simple)
if not args.simple:
print
print 'Download: %s Mbit/s' % round((dlspeed / 1024 / 1024) * 8, 2)
sizesizes = [int(.25 * 1024 * 1024), int(.5 * 1024 * 1024)]
sizes = []
for size in sizesizes:
for i in xrange(0, 25):
sizes.append(size)
if not args.simple:
print 'Testing upload speed',
ulspeed = uploadSpeed(best['url'], sizes)
print '\nUpload speed: %s Mbit/s' % round((ulspeed / 1024 / 1024) * 8, 2)
ulspeed = uploadSpeed(best['url'], sizes, args.simple)
if not args.simple:
print
print 'Upload: %s Mbit/s' % round((ulspeed / 1024 / 1024) * 8, 2)
if args.share:
dlspeedk = int(round((dlspeed / 1024) * 8, 0))
ping = int(round(best['latency'], 0))
ulspeedk = int(round((ulspeed / 1024) * 8, 0))
@ -308,7 +353,8 @@ def speedtest():
print 'Could not submit results to speedtest.net'
sys.exit(1)
print 'Share results: http://www.speedtest.net/result/%s.png' % resultid[0]
print ('Share results: http://www.speedtest.net/result/%s.png' %
resultid[0])
if __name__ == '__main__':
speedtest()

View File

@ -26,6 +26,7 @@ import sys
import hashlib
import threading
import binascii
import argparse
from queue import Queue
@ -70,7 +71,7 @@ class FileGetter(threading.Thread):
self.result = ''
def downloadSpeed(files):
def downloadSpeed(files, quiet=False):
start = time.time()
def producer(q, files):
@ -78,6 +79,7 @@ def downloadSpeed(files):
thread = FileGetter(file, start)
thread.start()
q.put(thread, True)
if not quiet:
sys.stdout.write('.')
sys.stdout.flush()
@ -127,7 +129,7 @@ class FilePutter(threading.Thread):
self.result = ''
def uploadSpeed(url, sizes):
def uploadSpeed(url, sizes, quiet=False):
start = time.time()
def producer(q, sizes):
@ -135,6 +137,7 @@ def uploadSpeed(url, sizes):
thread = FilePutter(url, start, size)
thread.start()
q.put(thread, True)
if not quiet:
sys.stdout.write('.')
sys.stdout.flush()
@ -235,15 +238,37 @@ def getBestServer(servers):
def speedtest():
"""Run the full speedtest.net test"""
description = (
'Command line interface for testing internet bandwidth using '
'speedtest.net.\n'
'------------------------------------------------------------'
'--------------\n'
'https://github.com/sivel/speedtest-cli')
parser = argparse.ArgumentParser(description=description)
parser.add_argument('--share', action='store_true',
help='Generate and provide a URL to the speedtest.net '
'share results image')
parser.add_argument('--simple', action='store_true',
help='Suppress verbose output, only show basic '
'information')
args = parser.parse_args()
if not args.simple:
print('Retrieving speedtest.net configuration...')
config = getConfig()
if not args.simple:
print('Retrieving speedtest.net server list...')
servers = closestServers(config['client'])
if not args.simple:
print('Selecting best server based on ping...')
best = getBestServer(servers)
if not args.simple:
print('Hosted by %(sponsor)s (%(name)s): %(latency)sms' % best)
else:
print('Ping: %(latency)sms' % best)
sizes = [350, 500, 750, 1000, 1500, 2000, 2500, 3000, 3500, 4000]
urls = []
@ -251,19 +276,26 @@ def speedtest():
for i in range(0, 4):
urls.append('%s/random%sx%s.jpg' % (os.path.dirname(best['url']),
size, size))
if not args.simple:
print('Testing download speed', end='')
dlspeed = downloadSpeed(urls)
print('\nDownload: %s Mbit/s' % round((dlspeed / 1024 / 1024) * 8, 2))
dlspeed = downloadSpeed(urls, args.simple)
if not args.simple:
print()
print('Download: %s Mbit/s' % round((dlspeed / 1024 / 1024) * 8, 2))
sizesizes = [int(.25 * 1024 * 1024), int(.5 * 1024 * 1024)]
sizes = []
for size in sizesizes:
for i in range(0, 25):
sizes.append(size)
if not args.simple:
print('Testing upload speed', end='')
ulspeed = uploadSpeed(best['url'], sizes)
print('\nUpload speed: %s Mbit/s' % round((ulspeed / 1024 / 1024) * 8, 2))
ulspeed = uploadSpeed(best['url'], sizes, args.simple)
if not args.simple:
print()
print('Upload speed: %s Mbit/s' % round((ulspeed / 1024 / 1024) * 8, 2))
if args.share:
dlspeedk = int(round((dlspeed / 1024) * 8, 0))
ping = int(round(best['latency'], 0))
ulspeedk = int(round((ulspeed / 1024) * 8, 0))
@ -299,8 +331,8 @@ def speedtest():
print('Could not submit results to speedtest.net')
sys.exit(1)
print('Share results: http://www.speedtest.net/result/%s.png' % resultid[0]
.decode())
print('Share results: http://www.speedtest.net/result/%s.png' %
resultid[0].decode())
if __name__ == '__main__':
speedtest()