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"