套接字使用特定的DNS服务器解析DNS [英] Socket resolve DNS with specific DNS server
本文介绍了套接字使用特定的DNS服务器解析DNS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用特定的DNS服务器来解析DNS,例如Google的8.8.8.8.我实际的Python代码是:
I want to resolve DNS with specific DNS server, for example Google's 8.8.8.8. My actual Python code is:
import socket
def getIP(d):
try:
data = socket.gethostbyname(d)
ip = repr(data)
return True
except Exception:
# fail gracefully!
return False
是否可以使用Python?
Is it possible using Python?
推荐答案
您可以使用dnspython: http://www.dnspython.org/在ubuntu/debian上,您可以使用:
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 via:
sudo pip install dnspython
或下载源代码,通过以下方式安装它:
Or download the source install it via:
sudo python setup.py install
您的代码将如下所示:
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)
由于OP在Mac OS X上使用时似乎有问题,因此,我做了以下操作来安装它(仅适用于本地用户):
Since the OP seems to have issues using it on Mac OS X here is what I did to get it installed (for local user only):
git clone git://github.com/rthalley/dnspython.git
cd dnspython
python setup.py install --user
这篇关于套接字使用特定的DNS服务器解析DNS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文