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
source
share