Skip to main content

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.

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": "localhost"})
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

Limitation

It works only with Python 3.4 and greater for the moment.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for hookdns, version 1.0.0
Filename, size File type Python version Upload date Hashes
Filename, size hookdns-1.0.0-py3-none-any.whl (9.3 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size hookdns-1.0.0.tar.gz (3.2 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page