netutils.py 文件源码

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

项目:deb-oslo.utils 作者: openstack 项目源码 文件源码
def _get_my_ipv4_address():
    """Figure out the best ipv4
    """
    LOCALHOST = '127.0.0.1'
    gtw = netifaces.gateways()
    try:
        interface = gtw['default'][netifaces.AF_INET][1]
    except (KeyError, IndexError):
        LOG.info(_LI('Could not determine default network interface, '
                     'using 127.0.0.1 for IPv4 address'))
        return LOCALHOST

    try:
        return netifaces.ifaddresses(interface)[netifaces.AF_INET][0]['addr']
    except (KeyError, IndexError):
        LOG.info(_LI('Could not determine IPv4 address for interface %s, '
                     'using 127.0.0.1'),
                 interface)
    except Exception as e:
        LOG.info(_LI('Could not determine IPv4 address for '
                     'interface %(interface)s: %(error)s'),
                 {'interface': interface, 'error': e})
    return LOCALHOST
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号