📜  python parse etc resolv.conf - Python 代码示例

📅  最后修改于: 2022-03-11 14:46:32.333000             🧑  作者: Mango

代码示例1
def get_resolvers() -> str:
    """
    if using WSL will access /etc/resolv.conf and parse the host address
    :return: str ip address
    """
    resolvers = []
    try:
        with open("/etc/resolv.conf", encoding='utf-8') as resolvconf:
            for line in resolvconf.readlines():
                line = line.split('#', 1)[0].rstrip()
        if 'nameserver' in line:
            resolvers.append(line.split()[1])
        return resolvers[0] if len(resolvers) > 0 else "127.0.0.1"
    except Exception as err:
        return "127.0.0.1"