23 lines
605 B
Python
23 lines
605 B
Python
"""Network utilities."""
|
|
from ipaddress import IPv4Address, IPv6Address, ip_address, ip_network
|
|
from typing import Union
|
|
|
|
# IP addresses of loopback interfaces
|
|
LOCAL_IPS = (
|
|
ip_address('127.0.0.1'),
|
|
ip_address('::1'),
|
|
)
|
|
|
|
# RFC1918 - Address allocation for Private Internets
|
|
LOCAL_NETWORKS = (
|
|
ip_network('10.0.0.0/8'),
|
|
ip_network('172.16.0.0/12'),
|
|
ip_network('192.168.0.0/16'),
|
|
)
|
|
|
|
|
|
def is_local(address: Union[IPv4Address, IPv6Address]) -> bool:
|
|
"""Check if an address is local."""
|
|
return address in LOCAL_IPS or \
|
|
any(address in network for network in LOCAL_NETWORKS)
|