dnspython
一个Python实现的一个DNS工具包,利用其查询功能来实现dns的服务监控及解析结果的校验。
安装
pip install dnspython
解析域名为IP
from dns import resolver
f = open('ddd.log', 'w')
with open('urls.txt', 'r') as fp:
for line in fp:
try:
url = line[7:].strip()
ans = resolver.query(url, 'A') # A记录,将主机名转换为IP地址;
# for x in ans.response.answer: # 获取结果
# for y in x.items:
# print(y)
t = url + '||Successful!'
except Exception as e:
t = url + '||' + str(e)
print('resolver error!')
finally:
f.write(t + '
')
f.close()
urls.txt
http://www.lyskjqbyjs.cn
http://www.haict.edu.cn
http://news.kf.cn
http://www.henanjubao.com
http://huixiangtan.smx.net.cn
http://www.hnjnjc.gov.cn
...
ddd.log
www.lyskjqbyjs.cn||Successful!
www.haict.edu.cn||None of DNS query names exist: www.haict.edu.cn., www.haict.edu.cn.
news.kf.cn||Successful!
www.henanjubao.com||Successful!
huixiangtan.smx.net.cn||Successful!
www.xcxnmj.gov.cn||None of DNS query names exist: www.xcxnmj.gov.cn., wx.hazz-l-
www.yhgq.gov.cn||Successful!
www.zzdangshi.com||Successful!
smx.hnwzj.gov.cn||Successful!
wfxy.ayit.edu.cn||Successful!
www.sqsglj.com||All nameservers failed to answer the query www.sqsglj.com. IN A: Server 127.0.0.53 UDP port 53 answered The DNS operation timed out.; Server 127.0.0.53 UDP port 53 answered The DNS operation timed out.; Server 127.0.0.53 UDP port 53 answered The DNS operation timed out.; Server 127.0.0.53 UDP port 53 answered The DNS operation timed out.; Server 127.0.0.53 UDP port 53 answered The DNS operation timed out.; Server 127.0.0.53 UDP port 53 answered The DNS operation timed out.; Server 127.0.0.53 UDP port 53 answered SERVFAIL
www.yyxsjj.gov.cn||Successful!
...
socket包的gethostbyname(URL)也能解析IP地址,jd不推荐用socket,推荐dnspython。