core.py 文件源码

python
阅读 23 收藏 0 点赞 0 评论 0

项目:networkzero 作者: tjguk 项目源码 文件源码
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
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号