@@ -413,6 +413,8 @@ def __init__(self, *args, **kwargs):
413413 source_address = kwargs .pop ('source_address' , None )
414414 timeout = kwargs .pop ('timeout' , 10 )
415415
416+ self ._tunnel_host = None
417+
416418 HTTPConnection .__init__ (self , * args , ** kwargs )
417419
418420 self .source_address = source_address
@@ -433,32 +435,54 @@ def connect(self):
433435 self .source_address
434436 )
435437
438+ if self ._tunnel_host :
439+ self ._tunnel ()
440+
436441
437442if HTTPSConnection :
438- class SpeedtestHTTPSConnection (HTTPSConnection ,
439- SpeedtestHTTPConnection ):
443+ class SpeedtestHTTPSConnection (HTTPSConnection ):
440444 """Custom HTTPSConnection to support source_address across
441445 Python 2.4 - Python 3
442446 """
447+ default_port = 443
448+
443449 def __init__ (self , * args , ** kwargs ):
444450 source_address = kwargs .pop ('source_address' , None )
445451 timeout = kwargs .pop ('timeout' , 10 )
446452
453+ self ._tunnel_host = None
454+
447455 HTTPSConnection .__init__ (self , * args , ** kwargs )
448456
449457 self .timeout = timeout
450458 self .source_address = source_address
451459
452460 def connect (self ):
453461 "Connect to a host on a given (SSL) port."
462+ try :
463+ self .sock = socket .create_connection (
464+ (self .host , self .port ),
465+ self .timeout ,
466+ self .source_address
467+ )
468+ except (AttributeError , TypeError ):
469+ self .sock = create_connection (
470+ (self .host , self .port ),
471+ self .timeout ,
472+ self .source_address
473+ )
454474
455- SpeedtestHTTPConnection .connect (self )
475+ if self ._tunnel_host :
476+ self ._tunnel ()
456477
457478 if ssl :
458479 try :
459480 kwargs = {}
460481 if hasattr (ssl , 'SSLContext' ):
461- kwargs ['server_hostname' ] = self .host
482+ if self ._tunnel_host :
483+ kwargs ['server_hostname' ] = self ._tunnel_host
484+ else :
485+ kwargs ['server_hostname' ] = self .host
462486 self .sock = self ._context .wrap_socket (self .sock , ** kwargs )
463487 except AttributeError :
464488 self .sock = ssl .wrap_socket (self .sock )
0 commit comments