gpt4 book ai didi

python - DNS 解析器不包含答案

转载 作者:行者123 更新时间:2023-12-01 06:30:33 25 4
gpt4 key购买 nike

我正在尝试使用 python 自动执行一些任务。我必须检查某些域是否仍在我们的 DNS 服务器中。因此,通过堆栈搜索,我找到了带有 dns.resolver 的脚本,并尝试使其满足我的需求。因此,脚本如下所示:

import socket
import dns.resolver


mylist=[]
with open('domainfile.txt') as file:
for line in file:
mylist.append(line)
length = len(mylist)


resolver = dns.resolver.Resolver()
resolver.nameservers=[socket.gethostbyname('dns.example.com')]

for i in range(length):
for rdata in resolver.query(mylist[i], 'CNAME') :
print(rdata.target)

我的domainfile.txt看起来像这样:

domain1
domain2
domain3
[...]

我收到的错误消息是:

Traceback (most recent call last): File "dnspython.py", line 20, in for rdata in resolver.query(lista[i], 'CNAME') : File "/usr/local/lib/python3.7/dist-packages/dns/resolver.py", line 1053, in query raise_on_no_answer) File "/usr/local/lib/python3.7/dist-packages/dns/resolver.py", line 236, in init raise NoAnswer(response=response) dns.resolver.NoAnswer: The DNS response does not contain an answer to the question: domain1\010.example.com. IN CNAME

如果我只是将我的域插入到if语句中,而不是遍历mylist,我就会得到我需要的东西。但是,当我尝试通过列表执行此操作时,它会添加到我的域\010,然后什么也没有发生。你能帮我解决这个问题吗?谢谢!

最佳答案

根据文档,这似乎是预期的:http://www.dnspython.org/docs/1.14.0/dns.resolver.Resolver-class.html#query

Raises:

...

  • NoAnswer - the response did not contain an answer and raise_on_no_answer is True.

发生这种情况的原因可能是因为您尚未从域中删除行结尾。

<小时/>

这就是我的做法。

import socket
import dns.resolver


with open('domainlist.txt') as f:
my_list = [line.strip() for line in f.readlines()]

resolver = dns.resolver.Resolver()
resolver.nameservers=[socket.gethostbyname('dns.example.com')]

for domain in my_list:
try:
q = resolver.query(domain, 'CNAME')
for rdata in q:
print(f'{domain}: {rdata.target}')
except dns.resolver.NoAnswer:
print(f'{domain}: No answer')

关于python - DNS 解析器不包含答案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59932579/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com