hosts_resolver/README.md

1.5 KiB

hosts_resolver

Zero-dependency Python module to resolve names using /etc/hosts alone

Rationale

Sometimes we only want to resolve names using /etc/hosts entries without performing DNS requests (thus socket.getaddrinfo is out of the equation).

So there are three options :

  • manually parse /etc/hosts
  • call getent program and parse its output
  • use this module (which binds libc gethostent function) !

Limitations

  • gethostent is not reentrant (see GETHOSTBYNAME(3))
  • gethostent is not thread-safe (see GETHOSTBYNAME(3))
  • glibc implementations does not support IPv6 addresses (see GETHOSTBYNAME(3))
  • behavior is undefined when using multiple context managers simultaneously

Installation

pip3 install --index-url https://git.forestier.app/api/packages/HorlogeSkynet/pypi/simple/ hosts-resolver

# or manually download latest Wheel and then :
pip3 install ./hosts_resolver-*-py3-none-any.whl

Usage

From API

from hosts_resolver.hosts_resolver import get_hosts_resolver

with get_hosts_resolver() as hosts_resolver:
    print(hosts_resolver.resolve("localhost"))

# -> IPv4Address('127.0.0.1')

From CLI

Why bother ?? You should be looking at getent !

hosts_resolver -j localhost
# -> {"localhost": "127.0.0.1"}

Development

pip3 install -r requirements-dev.txt

pylint src/
mypy src/
isort --check src/
black --check src/

python3 -m build

Any patch sent by e-mail to samuel+dev@forestier.app will get properly reviewed.