Socket resolves DNS with a specific DNS server

I want to resolve DNS with a specific DNS server, for example Google 8.8.8.8. My real Python code is:

import socket

def getIP(d):
    try:
        data = socket.gethostbyname(d)
        ip = repr(data)
        return True
    except Exception:
        # fail gracefully!
        return False

Is it possible to use Python?

+4
source share
1 answer

You can use dnspython: http://www.dnspython.org/ On ubuntu / debian you can get it using:

sudo apt-get install python-dnspython

Otherwise, get it through:

sudo pip install dnspython

Or download the source, install it through:

sudo python setup.py install

Your code will be something like this:

from dns import resolver

res = resolver.Resolver()
res.nameservers = ['8.8.8.8']

answers = res.query('stackexchange.com')

for rdata in answers:
    print (rdata.address)

Edit: Since the OP seems to have problems using it on Mac OS X, here is what I did to install it (local user only):

git clone git://github.com/rthalley/dnspython.git
cd dnspython
python setup.py install --user
+4
source

Source: https://habr.com/ru/post/1624298/


All Articles