Skip to content
This repository was archived by the owner on Jan 21, 2026. It is now read-only.

Commit 41e599f

Browse files
committed
Ensure we are utilizing the context created by HTTPSConnection, or falling back to ssl. Fixes #517
1 parent c7530bb commit 41e599f

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

speedtest.py

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
gzip = None
3737
GZIP_BASE = object
3838

39-
__version__ = '2.0.1'
39+
__version__ = '2.0.2a'
4040

4141

4242
class FakeShutdownEvent(object):
@@ -85,9 +85,9 @@ def isSet():
8585
HTTPErrorProcessor, OpenerDirector)
8686

8787
try:
88-
from httplib import HTTPConnection
88+
from httplib import HTTPConnection, BadStatusLine
8989
except ImportError:
90-
from http.client import HTTPConnection
90+
from http.client import HTTPConnection, BadStatusLine
9191

9292
try:
9393
from httplib import HTTPSConnection
@@ -266,10 +266,13 @@ def write(data):
266266
except AttributeError:
267267
CERT_ERROR = tuple()
268268

269-
HTTP_ERRORS = ((HTTPError, URLError, socket.error, ssl.SSLError) +
270-
CERT_ERROR)
269+
HTTP_ERRORS = (
270+
(HTTPError, URLError, socket.error, ssl.SSLError, BadStatusLine) +
271+
CERT_ERROR
272+
)
271273
except ImportError:
272-
HTTP_ERRORS = (HTTPError, URLError, socket.error)
274+
ssl = None
275+
HTTP_ERRORS = (HTTPError, URLError, socket.error, BadStatusLine)
273276

274277

275278
class SpeedtestException(Exception):
@@ -420,24 +423,26 @@ class SpeedtestHTTPSConnection(HTTPSConnection,
420423
"""
421424
def __init__(self, *args, **kwargs):
422425
source_address = kwargs.pop('source_address', None)
423-
context = kwargs.pop('context', None)
424426
timeout = kwargs.pop('timeout', 10)
425427

426428
HTTPSConnection.__init__(self, *args, **kwargs)
427429

428-
self.source_address = source_address
429-
self._context = context
430430
self.timeout = timeout
431+
self.source_address = source_address
431432

432433
def connect(self):
433434
"Connect to a host on a given (SSL) port."
434435

435436
SpeedtestHTTPConnection.connect(self)
436437

437438
kwargs = {}
438-
if hasattr(ssl, 'SSLContext'):
439-
kwargs['server_hostname'] = self.host
440-
self.sock = self._context.wrap_socket(self.sock, **kwargs)
439+
if ssl:
440+
if hasattr(ssl, 'SSLContext'):
441+
kwargs['server_hostname'] = self.host
442+
try:
443+
self.sock = self._context.wrap_socket(self.sock, **kwargs)
444+
except AttributeError:
445+
self.sock = ssl.wrap_socket(self.sock, **kwargs)
441446

442447

443448
def _build_connection(connection, source_address, timeout, context=None):

0 commit comments

Comments
 (0)