Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/cle-b/hookdns
An easy way to customize the dns resolution in your Python scripts.
https://github.com/cle-b/hookdns
dns python
Last synced: 13 days ago
JSON representation
An easy way to customize the dns resolution in your Python scripts.
- Host: GitHub
- URL: https://github.com/cle-b/hookdns
- Owner: cle-b
- License: apache-2.0
- Created: 2018-12-28T16:04:03.000Z (almost 6 years ago)
- Default Branch: main
- Last Pushed: 2024-10-23T20:31:15.000Z (27 days ago)
- Last Synced: 2024-10-25T23:07:08.883Z (25 days ago)
- Topics: dns, python
- Language: Python
- Homepage:
- Size: 44.9 KB
- Stars: 3
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[![Build Status](https://github.com/cle-b/hookdns/workflows/Build/badge.svg?branch=main)](https://github.com/cle-b/hookdns/actions?query=workflow%3ABuild) [![Coverage Status](https://coveralls.io/repos/github/cle-b/hookdns/badge.svg?branch=main)](https://coveralls.io/github/cle-b/hookdns?branch=main) [![PyPI version](https://badge.fury.io/py/hookdns.svg)](https://pypi.org/project/hookdns/)
# 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.
```python
import requestsfrom 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
```python
import requestsfrom 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
```python
import requestsfrom hookdns import hosts
with hosts({"example.org": "localhost"}):
...
r = requests.get("http://example.org") # the request is sent to your local server
...
```
### OptionsBy 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.
```python
import socketfrom 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
```