python parse 2fetc 2fresolv conf

Solutions on MaxInterview for python parse 2fetc 2fresolv conf by the best coders in the world

showing results for - "python parse 2fetc 2fresolv conf"
Martina
06 Oct 2017
1def get_resolvers() -> str:
2    """
3    if using WSL will access /etc/resolv.conf and parse the host address
4    :return: str ip address
5    """
6    resolvers = []
7    try:
8        with open("/etc/resolv.conf", encoding='utf-8') as resolvconf:
9            for line in resolvconf.readlines():
10                line = line.split('#', 1)[0].rstrip()
11        if 'nameserver' in line:
12            resolvers.append(line.split()[1])
13        return resolvers[0] if len(resolvers) > 0 else "127.0.0.1"
14    except Exception as err:
15        return "127.0.0.1"