def _find_ip4_addresses():
"""Find all the IP4 addresses currently bound to interfaces
"""
global _ip4_addresses
proto = socket.AF_INET
if _ip4_addresses is None:
_ip4_addresses = []
#
# Determine the interface for the default gateway
# (if any) and, later, prioritise the INET address on
# that interface.
#
default_gateway = netifaces.gateways()['default']
if proto in default_gateway:
_, default_gateway_interface = default_gateway[proto]
else:
default_gateway_interface = None
for interface in netifaces.interfaces():
for info in netifaces.ifaddresses(interface).get(netifaces.AF_INET, []):
if info['addr']:
if interface == default_gateway_interface:
_ip4_addresses.insert(0, info['addr'])
else:
_ip4_addresses.append(info['addr'])
return _ip4_addresses
评论列表
文章目录