def get_all_addresses_for_interface(interface: str) -> Iterable[str]:
"""Yield all IPv4 and IPv6 addresses for an interface as `IPAddress`es.
IPv4 addresses will be yielded first, followed by IPv6 addresses.
:param interface: The name of the interface whose addresses we
should retrieve.
"""
addresses = netifaces.ifaddresses(interface)
if netifaces.AF_INET in addresses:
for inet_address in addresses[netifaces.AF_INET]:
if "addr" in inet_address:
yield inet_address["addr"]
if netifaces.AF_INET6 in addresses:
for inet6_address in addresses[netifaces.AF_INET6]:
if "addr" in inet6_address:
# We know the interface name, so we don't care to keep the
# interface name on link-local addresses. Strip those off
# here.
yield clean_up_netifaces_address(
inet6_address["addr"], interface)
评论列表
文章目录