def get_host_ip4(iface_prefix=None, exclude_prefix=None):
if iface_prefix is None:
iface_prefix = ['']
if type(iface_prefix) in types.StringTypes:
iface_prefix = [iface_prefix]
if exclude_prefix is not None:
if type(exclude_prefix) in types.StringTypes:
exclude_prefix = [exclude_prefix]
ips = []
for ifacename in netifaces.interfaces():
matched = False
for t in iface_prefix:
if ifacename.startswith(t):
matched = True
break
if exclude_prefix is not None:
for ex in exclude_prefix:
if ifacename.startswith(ex):
matched = False
break
if not matched:
continue
addrs = netifaces.ifaddresses(ifacename)
if netifaces.AF_INET in addrs and netifaces.AF_LINK in addrs:
for addr in addrs[netifaces.AF_INET]:
ip = addr['addr']
if not is_ip4_loopback(ip):
ips.append(ip)
return ips
评论列表
文章目录