Dnspython:设置查询超时/生命周期
我有一个小脚本,用于检查大量域的 MX 记录,一切正常,但当脚本找到没有记录的域时,需要相当长的时间才能跳到下一个域。
我尝试添加:
query.lifetime = 1.0
or
query.timeout = 1.0
但这似乎没有任何作用。有谁知道这个设置是如何配置的?
我的脚本如下,感谢您的宝贵时间。
import dns.resolver
from dns.exception import DNSException
import dns.query
import csv
domains = csv.reader(open('domains.csv', 'rU'))
output = open('output.txt', 'w')
for row in domains:
try:
domain = row[0]
query = dns.resolver.query(domain,'MX')
query.lifetime = 1.0
except DNSException:
print "nothing here"
for rdata in query:
print domain, " ", rdata.exchange, 'has preference', rdata.preference
output.writelines(domain)
output.writelines(",")
output.writelines(rdata.exchange.to_text())
output.writelines("\n")
I have a small script that checks a large list of domains for their MX records, everything works fine but when the script finds a domain with no record, it takes quite a long time to skip to the next one.
I have tried adding:
query.lifetime = 1.0
or
query.timeout = 1.0
but this doesn't seem to do anything. Does anyone know how this setting is configured?
My script is below, thanks for your time.
import dns.resolver
from dns.exception import DNSException
import dns.query
import csv
domains = csv.reader(open('domains.csv', 'rU'))
output = open('output.txt', 'w')
for row in domains:
try:
domain = row[0]
query = dns.resolver.query(domain,'MX')
query.lifetime = 1.0
except DNSException:
print "nothing here"
for rdata in query:
print domain, " ", rdata.exchange, 'has preference', rdata.preference
output.writelines(domain)
output.writelines(",")
output.writelines(rdata.exchange.to_text())
output.writelines("\n")
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您在执行查询之后设置超时。所以这不会有任何作用!
您想要做的是创建一个 Resolver 对象,设置其超时,然后调用其 query() 方法。
dns.resolver.query()
只是一个便捷函数,它实例化默认的Resolver
对象并调用其query()
方法,因此您需要如果您不需要默认的解析器
,请手动执行此操作。然后在循环中使用它:
您应该能够对所有查询使用相同的 Resolver 对象。
You're setting the timeout after you've already performed the query. So that's not gonna do anything!
What you want to do instead is create a
Resolver
object, set its timeout, and then call itsquery()
method.dns.resolver.query()
is just a convenience function that instantiates a defaultResolver
object and invokes itsquery()
method, so you need to do that manually if you don't want a defaultResolver
.Then use this in your loop:
You should be able to use the same
Resolver
object for all queries.