Dnspython:设置查询超时/生命周期

发布于 2024-12-28 23:10:12 字数 861 浏览 2 评论 0原文

我有一个小脚本,用于检查大量域的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

帝王念 2025-01-04 23:10:12

您在执行查询之后设置超时。所以这不会有任何作用!

您想要做的是创建一个 Resolver 对象,设置其超时,然后调用其 query() 方法。 dns.resolver.query() 只是一个便捷函数,它实例化默认的 Resolver 对象并调用其 query() 方法,因此您需要如果您不需要默认的解析器,请手动执行此操作。

resolver = dns.resolver.Resolver()
resolver.timeout = 1
resolver.lifetime = 1

然后在循环中使用它:

try:
    domain = row[0]
    query = resolver.resolve(domain,'MX')
except:
    # etc.

您应该能够对所有查询使用相同的 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 its query() method. dns.resolver.query() is just a convenience function that instantiates a default Resolver object and invokes its query() method, so you need to do that manually if you don't want a default Resolver.

resolver = dns.resolver.Resolver()
resolver.timeout = 1
resolver.lifetime = 1

Then use this in your loop:

try:
    domain = row[0]
    query = resolver.resolve(domain,'MX')
except:
    # etc.

You should be able to use the same Resolver object for all queries.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文