An easy way to customize the dns resolution
Project Links
Meta
Author: cle-b
Requires Python: >=3.9
Classifiers
Programming Language
- Python :: 3
Operating System
- OS Independent
Development Status
- 5 - Production/Stable
Topic
- Internet :: Name Service (DNS)
hookdns
HookDNS is a library which allow you to modify a name resolution in your Python script without any modification in your hosts file or by using a fake DNS resolver.
import requests
from hookdns import hosts
with hosts({"example.org": "127.0.0.1"}):
...
r = requests.get("http://example.org") # the request is sent to your local server
...
Installation
pip install hookdns
Usage
Custom DNS resolutions are describe by a dictionnary where the keys are hostnames and the values the expected corresponding addresses.
{
"hostname1": "addr1",
"hostname2": "addr2"
}
hostname and addr could be a domain name or a string representation of an IPv4/IPV6.
Example using the patch as a decorator
import requests
from hookdns import hosts
@hosts({"example.org": "127.0.0.1"})
def myfunc():
...
r = requests.get("http://example.org") # the request is sent to your local server
...
Example using the patch as a context manager
import requests
from hookdns import hosts
with hosts({"example.org": "localhost"}):
...
r = requests.get("http://example.org") # the request is sent to your local server
...
2.0.2
Mar 28, 2026
2.0.1
Nov 22, 2025
2.0.0
Jan 12, 2025
1.1.1
Nov 04, 2023
1.1.0
Feb 25, 2023
1.0.0
Jan 12, 2019
Wheel compatibility matrix
Files in release
No dependencies