Custom HTTPConnection to support source_address across Python 2.4 - Python 3
| 415 | |
| 416 | |
| 417 | class SpeedtestHTTPConnection(HTTPConnection): |
| 418 | """Custom HTTPConnection to support source_address across |
| 419 | Python 2.4 - Python 3 |
| 420 | """ |
| 421 | def __init__(self, *args, **kwargs): |
| 422 | source_address = kwargs.pop('source_address', None) |
| 423 | timeout = kwargs.pop('timeout', 10) |
| 424 | |
| 425 | self._tunnel_host = None |
| 426 | |
| 427 | HTTPConnection.__init__(self, *args, **kwargs) |
| 428 | |
| 429 | self.source_address = source_address |
| 430 | self.timeout = timeout |
| 431 | |
| 432 | def connect(self): |
| 433 | """Connect to the host and port specified in __init__.""" |
| 434 | try: |
| 435 | self.sock = socket.create_connection( |
| 436 | (self.host, self.port), |
| 437 | self.timeout, |
| 438 | self.source_address |
| 439 | ) |
| 440 | except (AttributeError, TypeError): |
| 441 | self.sock = create_connection( |
| 442 | (self.host, self.port), |
| 443 | self.timeout, |
| 444 | self.source_address |
| 445 | ) |
| 446 | |
| 447 | if self._tunnel_host: |
| 448 | self._tunnel() |
| 449 | |
| 450 | |
| 451 | if HTTPSConnection: |