异步DNS解析程序测试

2024-05-19 11:02:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我想测试大量的IP来寻找开放的DNS解析程序。我正试图找到最有效的方法来并行处理。目前我正试图用twisted来完成这个任务。我希望有10或20个并行线程发送一个查询,以避免阻塞槽超时。在

Twisted有一个DNSDatagramProtocol,看起来很合适,但我就是不知道如何将它与Twisted“reactor”和“threads”设施结合起来,使其高效运行。在

我读了很多twisted文档,但我仍然不确定什么是最好的方法。在

有人能举个例子吗?在


Tags: 方法文档程序ipdnstwisted线程例子
2条回答

试试gevent,生成许多greenlet来做一个DNS解析。gevent还有一个不错的DNS解析API:http://www.gevent.org/gevent.dns.html

他们甚至有一个例子: https://github.com/gevent/gevent/blob/master/examples/dns_mass_resolve.py

下面是一个演示Twisted Names API的快速示例:

from sys import argv
from itertools import cycle
from pprint import pprint

from twisted.names import client
from twisted.internet.task import react
from twisted.internet.defer import gatherResults, inlineCallbacks

def query(reactor, server, name):
    # Create a new resolver that uses the given DNS server
    resolver = client.Resolver(
        resolv="/dev/null", servers=[(server, 53)], reactor=reactor)
    # Use it to do an A request for the name
    return resolver.lookupAddress(name)

@inlineCallbacks
def main(reactor, *names):
    # Here's some random DNS servers to which to issue requests.
    servers = ["4.2.2.1", "8.8.8.8"]

    # Handy trick to cycle through those servers forever
    next_server = cycle(servers).next

    # Issue queries for all the names given, alternating between servers.
    results = []
    for n in names:
        results.append(query(reactor, next_server(), n))
    # Wait for all the results
    results = yield gatherResults(results)
    # And report them
    pprint(zip(names, results))

if __name__ == '__main__':
    # Run the main program with the reactor going and pass names
    # from the command line arguments to be resolved
    react(main, argv[1:])

相关问题 更多 >

    热门问题