Compare commits

...

11 Commits

Author SHA1 Message Date
Adam Cruz
e715ef3008 Update 'README.rst' 2018-06-10 06:11:23 +08:00
Adam Cruz
9384255634 Update 'README.rst' 2018-06-10 06:10:39 +08:00
Matt Martz
72ed585c6f Bump to v2.0.2 2018-05-24 11:06:29 -05:00
Matt Martz
41e599f9c3 Ensure we are utilizing the context created by HTTPSConnection, or falling back to ssl. Fixes #517 2018-05-24 09:37:39 -05:00
Matt Martz
c7530bb143 Bump to 2.0.1 for release 2018-05-23 15:26:20 -05:00
Matt Martz
4ceae77401 Handle virtualenv and tox versions for py2.6/3.3 2018-05-22 16:38:10 -05:00
Matt Martz
9e185e8f88 Properly handle older HTTPSConnection. Fixes #513 2018-05-22 15:28:00 -05:00
Matt Martz
9c2977acfc Gracefully handle XML parsing errors. Fixes #490 #491 2018-03-09 09:46:10 -06:00
Matt Martz
f8aa20ecdf Move results.share() to ensure csv and json have access to it. Fixes #483 2018-02-20 14:59:08 -06:00
Matt Martz
8ff923b0fb Bump to 2.0.1a 2018-02-13 16:22:23 -06:00
Matt Martz
35c3ee20ed Exit with nicer error if lat/lon is not valid 2018-02-13 16:21:57 -06:00
3 changed files with 89 additions and 30 deletions

View File

@ -42,7 +42,10 @@ before_install:
install:
- if [[ $(echo "$TOXENV" | egrep -c "py32") != 0 ]]; then pip install setuptools==17.1.1; fi;
- if [[ $(echo "$TOXENV" | egrep -c "(py2[45]|py3[12])") != 0 ]]; then pip install virtualenv==1.7.2 tox==1.3; fi;
- if [[ $(echo "$TOXENV" | egrep -c "(py2[45]|py3[12])") == 0 ]]; then pip install tox; fi;
- if [[ $(echo "$TOXENV" | egrep -c "(py26|py33)") != 0 ]]; then pip install virtualenv==15.2.0 tox==2.9.1; fi;
- if [[ $(echo "$TOXENV" | egrep -c "(py2[456]|py3[123])") == 0 ]]; then pip install tox; fi;
script:
- tox

View File

@ -1,3 +1,7 @@
#Usage
$ curl -s https://git.spectre5.com/adamcruz/speedtest-cli-pub/raw/branch/master/speedtest.py | python -
speedtest-cli
=============

View File

@ -36,7 +36,7 @@ except ImportError:
gzip = None
GZIP_BASE = object
__version__ = '2.0.0'
__version__ = '2.0.2'
class FakeShutdownEvent(object):
@ -70,6 +70,7 @@ except ImportError:
import xml.etree.ElementTree as ET
except ImportError:
from xml.dom import minidom as DOM
from xml.parsers.expat import ExpatError
ET = None
try:
@ -84,9 +85,9 @@ except ImportError:
HTTPErrorProcessor, OpenerDirector)
try:
from httplib import HTTPConnection
from httplib import HTTPConnection, BadStatusLine
except ImportError:
from http.client import HTTPConnection
from http.client import HTTPConnection, BadStatusLine
try:
from httplib import HTTPSConnection
@ -265,10 +266,13 @@ try:
except AttributeError:
CERT_ERROR = tuple()
HTTP_ERRORS = ((HTTPError, URLError, socket.error, ssl.SSLError) +
CERT_ERROR)
HTTP_ERRORS = (
(HTTPError, URLError, socket.error, ssl.SSLError, BadStatusLine) +
CERT_ERROR
)
except ImportError:
HTTP_ERRORS = (HTTPError, URLError, socket.error)
ssl = None
HTTP_ERRORS = (HTTPError, URLError, socket.error, BadStatusLine)
class SpeedtestException(Exception):
@ -284,7 +288,11 @@ class SpeedtestHTTPError(SpeedtestException):
class SpeedtestConfigError(SpeedtestException):
"""Configuration provided is invalid"""
"""Configuration XML is invalid"""
class SpeedtestServersError(SpeedtestException):
"""Servers XML is invalid"""
class ConfigRetrievalError(SpeedtestHTTPError):
@ -384,13 +392,11 @@ class SpeedtestHTTPConnection(HTTPConnection):
"""
def __init__(self, *args, **kwargs):
source_address = kwargs.pop('source_address', None)
context = kwargs.pop('context', None)
timeout = kwargs.pop('timeout', 10)
HTTPConnection.__init__(self, *args, **kwargs)
self.source_address = source_address
self._context = context
self.timeout = timeout
def connect(self):
@ -415,16 +421,28 @@ if HTTPSConnection:
"""Custom HTTPSConnection to support source_address across
Python 2.4 - Python 3
"""
def __init__(self, *args, **kwargs):
source_address = kwargs.pop('source_address', None)
timeout = kwargs.pop('timeout', 10)
HTTPSConnection.__init__(self, *args, **kwargs)
self.timeout = timeout
self.source_address = source_address
def connect(self):
"Connect to a host on a given (SSL) port."
SpeedtestHTTPConnection.connect(self)
kwargs = {}
if hasattr(ssl, 'SSLContext'):
kwargs['server_hostname'] = self.host
self.sock = self._context.wrap_socket(self.sock, **kwargs)
if ssl:
if hasattr(ssl, 'SSLContext'):
kwargs['server_hostname'] = self.host
try:
self.sock = self._context.wrap_socket(self.sock, **kwargs)
except AttributeError:
self.sock = ssl.wrap_socket(self.sock, **kwargs)
def _build_connection(connection, source_address, timeout, context=None):
@ -1042,16 +1060,16 @@ class Speedtest(object):
uh, e = catch_request(request, opener=self._opener)
if e:
raise ConfigRetrievalError(e)
configxml = []
configxml_list = []
stream = get_response_stream(uh)
while 1:
try:
configxml.append(stream.read(1024))
configxml_list.append(stream.read(1024))
except (OSError, EOFError):
raise ConfigRetrievalError(get_exception())
if len(configxml[-1]) == 0:
if len(configxml_list[-1]) == 0:
break
stream.close()
uh.close()
@ -1059,10 +1077,18 @@ class Speedtest(object):
if int(uh.code) != 200:
return None
printer('Config XML:\n%s' % ''.encode().join(configxml), debug=True)
configxml = ''.encode().join(configxml_list)
printer('Config XML:\n%s' % configxml, debug=True)
try:
root = ET.fromstring(''.encode().join(configxml))
try:
root = ET.fromstring(configxml)
except ET.ParseError:
e = get_exception()
raise SpeedtestConfigError(
'Malformed speedtest.net configuration: %s' % e
)
server_config = root.find('server-config').attrib
download = root.find('download').attrib
upload = root.find('upload').attrib
@ -1070,7 +1096,13 @@ class Speedtest(object):
client = root.find('client').attrib
except AttributeError:
root = DOM.parseString(''.join(configxml))
try:
root = DOM.parseString(configxml)
except ExpatError:
e = get_exception()
raise SpeedtestConfigError(
'Malformed speedtest.net configuration: %s' % e
)
server_config = get_attributes_by_tag_name(root, 'server-config')
download = get_attributes_by_tag_name(root, 'download')
upload = get_attributes_by_tag_name(root, 'upload')
@ -1119,7 +1151,13 @@ class Speedtest(object):
'upload_max': upload_count * size_count
})
self.lat_lon = (float(client['lat']), float(client['lon']))
try:
self.lat_lon = (float(client['lat']), float(client['lon']))
except ValueError:
raise SpeedtestConfigError(
'Unknown location: lat=%r lon=%r' %
(client.get('lat'), client.get('lon'))
)
printer('Config:\n%r' % self.config, debug=True)
@ -1173,13 +1211,13 @@ class Speedtest(object):
stream = get_response_stream(uh)
serversxml = []
serversxml_list = []
while 1:
try:
serversxml.append(stream.read(1024))
serversxml_list.append(stream.read(1024))
except (OSError, EOFError):
raise ServersRetrievalError(get_exception())
if len(serversxml[-1]) == 0:
if len(serversxml_list[-1]) == 0:
break
stream.close()
@ -1188,15 +1226,28 @@ class Speedtest(object):
if int(uh.code) != 200:
raise ServersRetrievalError()
printer('Servers XML:\n%s' % ''.encode().join(serversxml),
debug=True)
serversxml = ''.encode().join(serversxml_list)
printer('Servers XML:\n%s' % serversxml, debug=True)
try:
try:
root = ET.fromstring(''.encode().join(serversxml))
try:
root = ET.fromstring(serversxml)
except ET.ParseError:
e = get_exception()
raise SpeedtestServersError(
'Malformed speedtest.net server list: %s' % e
)
elements = root.getiterator('server')
except AttributeError:
root = DOM.parseString(''.join(serversxml))
try:
root = DOM.parseString(serversxml)
except ExpatError:
e = get_exception()
raise SpeedtestServersError(
'Malformed speedtest.net server list: %s' % e
)
elements = root.getElementsByTagName('server')
except (SyntaxError, xml.parsers.expat.ExpatError):
raise ServersRetrievalError()
@ -1809,6 +1860,9 @@ def shell():
printer('Results:\n%r' % results.dict(), debug=True)
if not args.simple and args.share:
results.share()
if args.simple:
printer('Ping: %s ms\nDownload: %0.2f M%s/s\nUpload: %0.2f M%s/s' %
(results.ping,
@ -1819,8 +1873,6 @@ def shell():
elif args.csv:
printer(results.csv(delimiter=args.csv_delimiter))
elif args.json:
if args.share:
results.share()
printer(results.json())
if args.share and not machine_format: