Add hidden debug option, which prints debug information
This commit is contained in:
parent
abe85d85ff
commit
308c530f07
|
@ -46,7 +46,7 @@ USER_AGENT = None
|
||||||
SOURCE = None
|
SOURCE = None
|
||||||
SHUTDOWN_EVENT = FakeShutdownEvent()
|
SHUTDOWN_EVENT = FakeShutdownEvent()
|
||||||
SCHEME = 'http'
|
SCHEME = 'http'
|
||||||
|
DEBUG = False
|
||||||
|
|
||||||
# Used for bound_interface
|
# Used for bound_interface
|
||||||
SOCKET_SOCKET = socket.socket
|
SOCKET_SOCKET = socket.socket
|
||||||
|
@ -117,10 +117,12 @@ except ImportError:
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from argparse import ArgumentParser as ArgParser
|
from argparse import ArgumentParser as ArgParser
|
||||||
|
from argparse import SUPPRESS as ARG_SUPPRESS
|
||||||
PARSER_TYPE_INT = int
|
PARSER_TYPE_INT = int
|
||||||
PARSER_TYPE_STR = str
|
PARSER_TYPE_STR = str
|
||||||
except ImportError:
|
except ImportError:
|
||||||
from optparse import OptionParser as ArgParser
|
from optparse import OptionParser as ArgParser
|
||||||
|
from optparse import SUPPRESS_HELP as ARG_SUPPRESS
|
||||||
PARSER_TYPE_INT = 'int'
|
PARSER_TYPE_INT = 'int'
|
||||||
PARSER_TYPE_STR = 'string'
|
PARSER_TYPE_STR = 'string'
|
||||||
|
|
||||||
|
@ -317,6 +319,10 @@ def build_request(url, data=None, headers={}):
|
||||||
int(timeit.time.time() * 1000))
|
int(timeit.time.time() * 1000))
|
||||||
|
|
||||||
headers['User-Agent'] = USER_AGENT
|
headers['User-Agent'] = USER_AGENT
|
||||||
|
|
||||||
|
printer('%s %s' % (('GET', 'POST')[bool(data)], final_url),
|
||||||
|
debug=True)
|
||||||
|
|
||||||
return Request(final_url, data=data, headers=headers)
|
return Request(final_url, data=data, headers=headers)
|
||||||
|
|
||||||
|
|
||||||
|
@ -612,6 +618,8 @@ class Speedtest(object):
|
||||||
|
|
||||||
uh.close()
|
uh.close()
|
||||||
|
|
||||||
|
printer(''.encode().join(configxml), debug=True)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
root = ET.fromstring(''.encode().join(configxml))
|
root = ET.fromstring(''.encode().join(configxml))
|
||||||
server_config = root.find('server-config').attrib
|
server_config = root.find('server-config').attrib
|
||||||
|
@ -759,6 +767,8 @@ class Speedtest(object):
|
||||||
except KeyError:
|
except KeyError:
|
||||||
self.servers[d] = [attrib]
|
self.servers[d] = [attrib]
|
||||||
|
|
||||||
|
printer(''.encode().join(serversxml), debug=True)
|
||||||
|
|
||||||
del root
|
del root
|
||||||
del serversxml
|
del serversxml
|
||||||
del elements
|
del elements
|
||||||
|
@ -840,6 +850,7 @@ class Speedtest(object):
|
||||||
continue
|
continue
|
||||||
break
|
break
|
||||||
|
|
||||||
|
printer(self.closest, debug=True)
|
||||||
return self.closest
|
return self.closest
|
||||||
|
|
||||||
def get_best_server(self, servers=[]):
|
def get_best_server(self, servers=[]):
|
||||||
|
@ -890,6 +901,7 @@ class Speedtest(object):
|
||||||
self.results.server = best
|
self.results.server = best
|
||||||
|
|
||||||
self.best.update(best)
|
self.best.update(best)
|
||||||
|
printer(best, debug=True)
|
||||||
return best
|
return best
|
||||||
|
|
||||||
def download(self, callback=None):
|
def download(self, callback=None):
|
||||||
|
@ -1065,6 +1077,8 @@ def parse_args():
|
||||||
'with speedtest.net operated servers')
|
'with speedtest.net operated servers')
|
||||||
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')
|
||||||
|
parser.add_argument('--debug', action='store_true',
|
||||||
|
help=ARG_SUPPRESS, default=ARG_SUPPRESS)
|
||||||
|
|
||||||
options = parser.parse_args()
|
options = parser.parse_args()
|
||||||
if isinstance(options, tuple):
|
if isinstance(options, tuple):
|
||||||
|
@ -1091,17 +1105,25 @@ def validate_optional_args(args):
|
||||||
'unavailable' % (info[0], arg))
|
'unavailable' % (info[0], arg))
|
||||||
|
|
||||||
|
|
||||||
def printer(string, quiet=False, **kwargs):
|
def printer(string, quiet=False, debug=False, **kwargs):
|
||||||
"""Helper function to print a string only when not quiet"""
|
"""Helper function to print a string only when not quiet"""
|
||||||
|
|
||||||
|
if debug and not DEBUG:
|
||||||
|
return
|
||||||
|
|
||||||
|
if debug:
|
||||||
|
out = '\033[1;30mDEBUG: %s\033[0m' % string
|
||||||
|
else:
|
||||||
|
out = string
|
||||||
|
|
||||||
if not quiet:
|
if not quiet:
|
||||||
print_(string, **kwargs)
|
print_(out, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
def shell():
|
def shell():
|
||||||
"""Run the full speedtest.net test"""
|
"""Run the full speedtest.net test"""
|
||||||
|
|
||||||
global SHUTDOWN_EVENT, SOURCE, SCHEME
|
global SHUTDOWN_EVENT, SOURCE, SCHEME, DEBUG
|
||||||
SHUTDOWN_EVENT = threading.Event()
|
SHUTDOWN_EVENT = threading.Event()
|
||||||
|
|
||||||
signal.signal(signal.SIGINT, ctrl_c)
|
signal.signal(signal.SIGINT, ctrl_c)
|
||||||
|
@ -1130,13 +1152,17 @@ def shell():
|
||||||
if args.secure:
|
if args.secure:
|
||||||
SCHEME = 'https'
|
SCHEME = 'https'
|
||||||
|
|
||||||
|
debug = getattr(args, 'debug', False)
|
||||||
|
if debug:
|
||||||
|
DEBUG = True
|
||||||
|
|
||||||
if args.simple or args.csv or args.json:
|
if args.simple or args.csv or args.json:
|
||||||
quiet = True
|
quiet = True
|
||||||
else:
|
else:
|
||||||
quiet = False
|
quiet = False
|
||||||
|
|
||||||
# Don't set a callback if we are running quietly
|
# Don't set a callback if we are running quietly
|
||||||
if quiet:
|
if quiet or debug:
|
||||||
callback = None
|
callback = None
|
||||||
else:
|
else:
|
||||||
callback = print_dots
|
callback = print_dots
|
||||||
|
@ -1212,13 +1238,13 @@ def shell():
|
||||||
printer('Hosted by %(sponsor)s (%(name)s) [%(d)0.2f km]: '
|
printer('Hosted by %(sponsor)s (%(name)s) [%(d)0.2f km]: '
|
||||||
'%(latency)s ms' % results.server, quiet)
|
'%(latency)s ms' % results.server, quiet)
|
||||||
|
|
||||||
printer('Testing download speed', quiet, end='')
|
printer('Testing download speed', quiet, end=('\n', '')[bool(callback)])
|
||||||
speedtest.download(callback=callback)
|
speedtest.download(callback=callback)
|
||||||
printer('Download: %0.2f M%s/s' %
|
printer('Download: %0.2f M%s/s' %
|
||||||
((results.download / 1000 / 1000) * args.units[1], args.units[0]),
|
((results.download / 1000 / 1000) * args.units[1], args.units[0]),
|
||||||
quiet)
|
quiet)
|
||||||
|
|
||||||
printer('Testing upload speed', quiet, end='')
|
printer('Testing upload speed', quiet, end=('\n', '')[bool(callback)])
|
||||||
speedtest.upload(callback=callback)
|
speedtest.upload(callback=callback)
|
||||||
printer('Upload: %0.2f M%s/s' %
|
printer('Upload: %0.2f M%s/s' %
|
||||||
((results.upload / 1000 / 1000) * args.units[1], args.units[0]),
|
((results.upload / 1000 / 1000) * args.units[1], args.units[0]),
|
||||||
|
|
Loading…
Reference in New Issue