|
31 | 31 | from socket import create_connection, SHUT_RDWR, error as SocketError |
32 | 32 | from struct import pack as struct_pack, unpack as struct_unpack, unpack_from as struct_unpack_from |
33 | 33 |
|
34 | | -import errno |
35 | | - |
36 | 34 | from .constants import DEFAULT_PORT, DEFAULT_USER_AGENT, KNOWN_HOSTS, MAGIC_PREAMBLE, \ |
37 | 35 | TRUST_DEFAULT, TRUST_ON_FIRST_USE |
38 | 36 | from .compat import hex2 |
@@ -239,6 +237,13 @@ def on_failure(metadata): |
239 | 237 | def __del__(self): |
240 | 238 | self.close() |
241 | 239 |
|
| 240 | + @property |
| 241 | + def healthy(self): |
| 242 | + """ Return ``True`` if this connection is healthy, ``False`` if |
| 243 | + unhealthy and ``None`` if closed. |
| 244 | + """ |
| 245 | + return None if self.closed else not self.defunct |
| 246 | + |
242 | 247 | def append(self, signature, fields=(), response=None): |
243 | 248 | """ Add a message to the outgoing queue. |
244 | 249 |
|
@@ -333,6 +338,12 @@ def fetch(self): |
333 | 338 | handler(*fields) |
334 | 339 | raw.close() |
335 | 340 |
|
| 341 | + def fetch_all(self): |
| 342 | + while self.responses: |
| 343 | + response = self.responses[0] |
| 344 | + while not response.complete: |
| 345 | + self.fetch() |
| 346 | + |
336 | 347 | def close(self): |
337 | 348 | """ Close the connection. |
338 | 349 | """ |
@@ -389,26 +400,26 @@ def match_or_trust(self, host, der_encoded_certificate): |
389 | 400 | return True |
390 | 401 |
|
391 | 402 |
|
392 | | -def connect(host, port=None, ssl_context=None, **config): |
| 403 | +def connect(host_port, ssl_context=None, **config): |
393 | 404 | """ Connect and perform a handshake and return a valid Connection object, assuming |
394 | 405 | a protocol version can be agreed. |
395 | 406 | """ |
396 | 407 |
|
397 | 408 | # Establish a connection to the host and port specified |
398 | 409 | # Catches refused connections see: |
399 | 410 | # https://docs.python.org/2/library/errno.html |
400 | | - port = port or DEFAULT_PORT |
401 | | - if __debug__: log_info("~~ [CONNECT] %s %d", host, port) |
| 411 | + if __debug__: log_info("~~ [CONNECT] %s", host_port) |
402 | 412 | try: |
403 | | - s = create_connection((host, port)) |
| 413 | + s = create_connection(host_port) |
404 | 414 | except SocketError as error: |
405 | 415 | if error.errno == 111 or error.errno == 61: |
406 | | - raise ProtocolError("Unable to connect to %s on port %d - is the server running?" % (host, port)) |
| 416 | + raise ProtocolError("Unable to connect to %s on port %d - is the server running?" % host_port) |
407 | 417 | else: |
408 | 418 | raise |
409 | 419 |
|
410 | 420 | # Secure the connection if an SSL context has been provided |
411 | 421 | if ssl_context and SSL_AVAILABLE: |
| 422 | + host, port = host_port |
412 | 423 | if __debug__: log_info("~~ [SECURE] %s", host) |
413 | 424 | try: |
414 | 425 | s = ssl_context.wrap_socket(s, server_hostname=host if HAS_SNI else None) |
|
0 commit comments