hass-core/homeassistant/util/network.py
Paulus Schoutsen f5632a5da5
Add webhook + IFTTT example (#16817)
* Add webhook + IFTTT example

* Abort if not externally accessible

* Abort on local url

* Add description to create entry

* Make body optional

* Allow ifttt setup without config

* Add tests

* Lint

* Fix Lint + Tests

* Fix typing
2018-09-30 14:45:48 +02:00

22 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)