🐍 Zero-dependency Python module to resolve names using /etc/hosts alone https://git.forestier.app/HorlogeSkynet/hosts_resolver
Go to file
Samuel FORESTIER 0ada516ac2 Moves to new personal domain name 2023-11-03 21:22:36 +01:00
src/hosts_resolver Bumps to v1.1.0 2023-06-17 23:05:38 +02:00
.gitignore Initial commit 2023-06-13 23:03:09 +02:00
LICENSE Initial commit 2023-06-13 23:03:09 +02:00
README.md Moves to new personal domain name 2023-11-03 21:22:36 +01:00
pyproject.toml Moves to new personal domain name 2023-11-03 21:22:36 +01:00
requirements-dev.txt Initial commit 2023-06-13 23:03:09 +02:00
setup.py Initial commit 2023-06-13 23:03:09 +02:00

README.md

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.