More and better debugging

This commit is contained in:
Matt Martz 2017-05-03 17:17:00 -05:00
parent f2a97baf1e
commit 8854d82049
1 changed files with 24 additions and 14 deletions

View File

@ -483,8 +483,12 @@ def build_opener(source_address=None, timeout=10):
`User-Agent` `User-Agent`
""" """
printer('Timeout set to %d' % timeout, debug=True)
if source_address: if source_address:
source_address_tuple = (source_address, 0) source_address_tuple = (source_address, 0)
printer('Binding to source address: %r' % (source_address_tuple,),
debug=True)
else: else:
source_address_tuple = None source_address_tuple = None
@ -575,11 +579,11 @@ def build_user_agent():
'speedtest-cli/%s' % __version__ 'speedtest-cli/%s' % __version__
) )
user_agent = ' '.join(ua_tuple) user_agent = ' '.join(ua_tuple)
printer(user_agent, debug=True) printer('User-Agent: %s' % user_agent, debug=True)
return user_agent return user_agent
def build_request(url, data=None, headers=None, bump='', secure=False): def build_request(url, data=None, headers=None, bump='0', secure=False):
"""Build a urllib2 request object """Build a urllib2 request object
This function automatically adds a User-Agent header to all requests This function automatically adds a User-Agent header to all requests
@ -980,7 +984,7 @@ class Speedtest(object):
if int(uh.code) != 200: if int(uh.code) != 200:
return None return None
printer(''.encode().join(configxml), debug=True) printer('Config XML:\n%s' % ''.encode().join(configxml), debug=True)
try: try:
root = ET.fromstring(''.encode().join(configxml)) root = ET.fromstring(''.encode().join(configxml))
@ -1042,7 +1046,7 @@ class Speedtest(object):
self.lat_lon = (float(client['lat']), float(client['lon'])) self.lat_lon = (float(client['lat']), float(client['lon']))
printer(self.config, debug=True) printer('Config:\n%r' % self.config, debug=True)
return self.config return self.config
@ -1101,7 +1105,8 @@ class Speedtest(object):
if int(uh.code) != 200: if int(uh.code) != 200:
raise ServersRetrievalError() raise ServersRetrievalError()
printer(''.encode().join(serversxml), debug=True) printer('Servers XML:\n%s' % ''.encode().join(serversxml),
debug=True)
try: try:
try: try:
@ -1139,8 +1144,6 @@ class Speedtest(object):
except KeyError: except KeyError:
self.servers[d] = [attrib] self.servers[d] = [attrib]
printer(''.encode().join(serversxml), debug=True)
break break
except ServersRetrievalError: except ServersRetrievalError:
@ -1222,7 +1225,7 @@ class Speedtest(object):
continue continue
break break
printer(self.closest, debug=True) printer('Closest Servers:\n%r' % self.closest, debug=True)
return self.closest return self.closest
def get_best_server(self, servers=None): def get_best_server(self, servers=None):
@ -1246,9 +1249,13 @@ class Speedtest(object):
for server in servers: for server in servers:
cum = [] cum = []
url = os.path.dirname(server['url']) url = os.path.dirname(server['url'])
urlparts = urlparse('%s/latency.txt' % url) stamp = int(timeit.time.time() * 1000)
printer('%s %s/latency.txt' % ('GET', url), debug=True) latency_url = '%s/latency.txt?x=%s' % (url, stamp)
for _ in range(0, 3): for i in range(0, 3):
this_latency_url = '%s.%s' % (latency_url, i)
printer('%s %s' % ('GET', this_latency_url),
debug=True)
urlparts = urlparse(latency_url)
try: try:
if urlparts[0] == 'https': if urlparts[0] == 'https':
h = SpeedtestHTTPSConnection( h = SpeedtestHTTPSConnection(
@ -1261,13 +1268,14 @@ class Speedtest(object):
source_address=source_address_tuple source_address=source_address_tuple
) )
headers = {'User-Agent': user_agent} headers = {'User-Agent': user_agent}
path = '%s?%s' % (urlparts[2], urlparts[4])
start = timeit.default_timer() start = timeit.default_timer()
h.request("GET", urlparts[2], headers=headers) h.request("GET", path, headers=headers)
r = h.getresponse() r = h.getresponse()
total = (timeit.default_timer() - start) total = (timeit.default_timer() - start)
except HTTP_ERRORS: except HTTP_ERRORS:
e = get_exception() e = get_exception()
printer('%r' % e, debug=True) printer('ERROR: %r' % e, debug=True)
cum.append(3600) cum.append(3600)
continue continue
@ -1293,7 +1301,7 @@ class Speedtest(object):
self.results.server = best self.results.server = best
self.best.update(best) self.best.update(best)
printer(best, debug=True) printer('Best Server:\n%r' % best, debug=True)
return best return best
def download(self, callback=do_nothing): def download(self, callback=do_nothing):
@ -1690,6 +1698,8 @@ def shell():
else: else:
printer('Skipping upload test') printer('Skipping upload test')
printer('Results:\n%r' % results.dict(), debug=True)
if args.simple: if args.simple:
print_('Ping: %s ms\nDownload: %0.2f M%s/s\nUpload: %0.2f M%s/s' % print_('Ping: %s ms\nDownload: %0.2f M%s/s\nUpload: %0.2f M%s/s' %
(results.ping, (results.ping,