2

我想测试大量 IP 以寻找开放的 DNS 解析器。我正在尝试找到最有效的方法来并行化它。目前我正试图通过扭曲来实现这一点。我希望有 10 或 20 个并行线程发送查询以避免阻塞槽超时。

Twisted 有一个似乎合适的 DNSDatagramProtocol,但我只是不知道如何将它与扭曲的“反应器”和“线程”设施组合在一起以使其高效运行。

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

有人可以举例说明如何实现吗?

4

2 回答 2

3

这是一个演示 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:])
于 2013-04-03T15:43:47.347 回答
-1

尝试 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

于 2013-04-03T14:31:13.927 回答