Returns True if the system can bind an IPv6 address.
(host: str)
| 56 | |
| 57 | |
| 58 | def _has_ipv6(host: str) -> bool: |
| 59 | """Returns True if the system can bind an IPv6 address.""" |
| 60 | sock = None |
| 61 | has_ipv6 = False |
| 62 | |
| 63 | if socket.has_ipv6: |
| 64 | # has_ipv6 returns true if cPython was compiled with IPv6 support. |
| 65 | # It does not tell us if the system has IPv6 support enabled. To |
| 66 | # determine that we must bind to an IPv6 address. |
| 67 | # https://github.com/urllib3/urllib3/pull/611 |
| 68 | # https://bugs.python.org/issue658327 |
| 69 | try: |
| 70 | sock = socket.socket(socket.AF_INET6) |
| 71 | sock.bind((host, 0)) |
| 72 | has_ipv6 = _resolves_to_ipv6("localhost") |
| 73 | except Exception: |
| 74 | pass |
| 75 | |
| 76 | if sock: |
| 77 | sock.close() |
| 78 | return has_ipv6 |
| 79 | |
| 80 | |
| 81 | # Some systems may have IPv6 support but DNS may not be configured |
no test coverage detected