An easy way to customize the dns resolution
Project description
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
...
Options
By default the following function calls are intercepted: socket.gethostbyname, socket.gethostbyname_ex and socket.getaddrinfo.
You can limit the interception to only a restricted list of function.
import socket
from hookdns import hosts
with hosts({"example.org": "localhost"}, only=["gethostbyname"]):
...
addr = socket.gethostbyname("example.org") # returns "127.0.0.1"
print("gethostname returns: %s" % addr)
_, _, addr = socket.gethostbyname_ex("example.org") # returns the real ip address for example.org
print("gethostname_ex returns: %s" % addr[0])
...
gethostname returns: 127.0.0.1
gethostname_ex returns: 93.184.216.34
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file hookdns-1.1.1.tar.gz
.
File metadata
- Download URL: hookdns-1.1.1.tar.gz
- Upload date:
- Size: 10.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4c6eb85e58b274a32d97cea55050ac3daa0a2c2238b6eb396a6a4962ab58297e |
|
MD5 | 702d88fbe7064a6f5abd06c7c140b6b1 |
|
BLAKE2b-256 | 072fb40129182c506502d6f34d8aa7b3e92ee6cf51b1612997024f4b0058175c |
File details
Details for the file hookdns-1.1.1-py3-none-any.whl
.
File metadata
- Download URL: hookdns-1.1.1-py3-none-any.whl
- Upload date:
- Size: 9.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ddd0036a10ed51eb8a7c5a5bfa3a1f89d236d81486e997fb009ae08658279b22 |
|
MD5 | da09fb30f0037f836311e34a524e5b39 |
|
BLAKE2b-256 | 56a04c0e81fd9905a383db075e1d5b38d9a8e57177286ca97b255b3faeb893ab |