Python 不导入 dns.resolver 中的列表
Python does not import list in dns.resolver
我正在编写一个代码来解析域列表中的 CNAME,但我当前的脚本只解析 list.txt
的第一行并跳过其余部分。
我检查 "dl"
通过 运行 print(dl)
将所有行导入列表,但 dns.resolver 不是 运行 检查每个列表数据。
我的示例代码是:
#/usr/bin/python3
import dns.resolver
d = open("list.txt" , "r")
dl = d.read().splitlines()
try:
for dom in dl:
answer = dns.resolver.resolve(dom, 'CNAME')
for rdata in answer:
print("CNAME", ':', rdata.to_text())
except Exception as e:
print(e)
list.txt
是:
www.google.com
www.github.com
www.facebook.com
期望的输出是:
CNAME : www.gcaname.com
CNAME : www.gcaname.com
The DNS response does not contain an answer to the question: www.github.com. IN CNAME
试试这个:
answers = dns.resolver.query('mail.google.com', 'CNAME')
print ' query qname:', answers.qname, ' num ans.', len(answers)
for rdata in answers:
print ' cname target address:', rdata.target
这有效:
for dom in dl:
try:
answer = dns.resolver.resolve(dom, 'CNAME')
for rdata in answer:
print("CNAME of %s" % dom, ':', rdata.to_text())
except Exception as e:
pass
我正在编写一个代码来解析域列表中的 CNAME,但我当前的脚本只解析 list.txt
的第一行并跳过其余部分。
我检查 "dl"
通过 运行 print(dl)
将所有行导入列表,但 dns.resolver 不是 运行 检查每个列表数据。
我的示例代码是:
#/usr/bin/python3
import dns.resolver
d = open("list.txt" , "r")
dl = d.read().splitlines()
try:
for dom in dl:
answer = dns.resolver.resolve(dom, 'CNAME')
for rdata in answer:
print("CNAME", ':', rdata.to_text())
except Exception as e:
print(e)
list.txt
是:
www.google.com
www.github.com
www.facebook.com
期望的输出是:
CNAME : www.gcaname.com
CNAME : www.gcaname.com
The DNS response does not contain an answer to the question: www.github.com. IN CNAME
试试这个:
answers = dns.resolver.query('mail.google.com', 'CNAME')
print ' query qname:', answers.qname, ' num ans.', len(answers)
for rdata in answers:
print ' cname target address:', rdata.target
这有效:
for dom in dl:
try:
answer = dns.resolver.resolve(dom, 'CNAME')
for rdata in answer:
print("CNAME of %s" % dom, ':', rdata.to_text())
except Exception as e:
pass