Answers for "python parse /etc/resolv.conf"

0

python parse /etc/resolv.conf

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"
Posted by: Guest on September-19-2021

Python Answers by Framework

Browse Popular Code Answers by Language