def get_lo_alias_addr():
# check all interfaces an try to find one with address 127.0.0.1
# If that interface has another address, that's the one we need.
ifname_loopback = None
for interface in netifaces.interfaces():
ip_addresses = []
interface_addresses = netifaces.ifaddresses(interface)
if netifaces.AF_INET not in interface_addresses:
continue
for address_data in interface_addresses[netifaces.AF_INET]:
if address_data.get('addr') == '127.0.0.1':
ifname_loopback = interface
elif address_data.get('addr'):
ip_addresses.append(address_data.get('addr'))
if interface == ifname_loopback and ip_addresses:
return ip_addresses[0]
评论列表
文章目录