Use stdlib ip_address method in the network helper when compatible (#102019)
This commit is contained in:
parent
c60cc11505
commit
d0ba42283c
1 changed files with 6 additions and 4 deletions
|
@ -7,10 +7,12 @@ import re
|
||||||
import yarl
|
import yarl
|
||||||
|
|
||||||
# RFC6890 - IP addresses of loopback interfaces
|
# RFC6890 - IP addresses of loopback interfaces
|
||||||
|
IPV6_IPV4_LOOPBACK = ip_network("::ffff:127.0.0.0/104")
|
||||||
|
|
||||||
LOOPBACK_NETWORKS = (
|
LOOPBACK_NETWORKS = (
|
||||||
ip_network("127.0.0.0/8"),
|
ip_network("127.0.0.0/8"),
|
||||||
ip_network("::1/128"),
|
ip_network("::1/128"),
|
||||||
ip_network("::ffff:127.0.0.0/104"),
|
IPV6_IPV4_LOOPBACK,
|
||||||
)
|
)
|
||||||
|
|
||||||
# RFC6890 - Address allocation for Private Internets
|
# RFC6890 - Address allocation for Private Internets
|
||||||
|
@ -34,7 +36,7 @@ LINK_LOCAL_NETWORKS = (
|
||||||
|
|
||||||
def is_loopback(address: IPv4Address | IPv6Address) -> bool:
|
def is_loopback(address: IPv4Address | IPv6Address) -> bool:
|
||||||
"""Check if an address is a loopback address."""
|
"""Check if an address is a loopback address."""
|
||||||
return any(address in network for network in LOOPBACK_NETWORKS)
|
return address.is_loopback or address in IPV6_IPV4_LOOPBACK
|
||||||
|
|
||||||
|
|
||||||
def is_private(address: IPv4Address | IPv6Address) -> bool:
|
def is_private(address: IPv4Address | IPv6Address) -> bool:
|
||||||
|
@ -44,7 +46,7 @@ def is_private(address: IPv4Address | IPv6Address) -> bool:
|
||||||
|
|
||||||
def is_link_local(address: IPv4Address | IPv6Address) -> bool:
|
def is_link_local(address: IPv4Address | IPv6Address) -> bool:
|
||||||
"""Check if an address is link-local (local but not necessarily unique)."""
|
"""Check if an address is link-local (local but not necessarily unique)."""
|
||||||
return any(address in network for network in LINK_LOCAL_NETWORKS)
|
return address.is_link_local
|
||||||
|
|
||||||
|
|
||||||
def is_local(address: IPv4Address | IPv6Address) -> bool:
|
def is_local(address: IPv4Address | IPv6Address) -> bool:
|
||||||
|
@ -54,7 +56,7 @@ def is_local(address: IPv4Address | IPv6Address) -> bool:
|
||||||
|
|
||||||
def is_invalid(address: IPv4Address | IPv6Address) -> bool:
|
def is_invalid(address: IPv4Address | IPv6Address) -> bool:
|
||||||
"""Check if an address is invalid."""
|
"""Check if an address is invalid."""
|
||||||
return bool(address == ip_address("0.0.0.0"))
|
return address.is_unspecified
|
||||||
|
|
||||||
|
|
||||||
def is_ip_address(address: str) -> bool:
|
def is_ip_address(address: str) -> bool:
|
||||||
|
|
Loading…
Add table
Reference in a new issue