This function accepts IPv4/IPv6 tuples and returns the formatted address string with port number
(address: tuple | None)
| 79 | |
| 80 | @functools.lru_cache |
| 81 | def format_address(address: tuple | None) -> str: |
| 82 | """ |
| 83 | This function accepts IPv4/IPv6 tuples and |
| 84 | returns the formatted address string with port number |
| 85 | """ |
| 86 | if address is None: |
| 87 | return "<no address>" |
| 88 | try: |
| 89 | host = ipaddress.ip_address(address[0]) |
| 90 | if host.is_unspecified: |
| 91 | return f"*:{address[1]}" |
| 92 | if isinstance(host, ipaddress.IPv4Address): |
| 93 | return f"{host}:{address[1]}" |
| 94 | # If IPv6 is mapped to IPv4 |
| 95 | elif host.ipv4_mapped: |
| 96 | return f"{host.ipv4_mapped}:{address[1]}" |
| 97 | return f"[{host}]:{address[1]}" |
| 98 | except ValueError: |
| 99 | return f"{address[0]}:{address[1]}" |
nothing calls this directly
no test coverage detected
searching dependent graphs…