我想测试大量 IP 以寻找开放的 DNS 解析器。我正在尝试找到最有效的方法来并行化它。目前我正在尝试用twisted 来实现这一点。我希望有 10 或 20 个并行线程发送查询以避免阻塞超时。
Twisted 有一个看起来合适的 DNSDatagramProtocol,但我只是不知道如何将它与扭曲的“ react 器”和“线程”设施放在一起以使其高效运行。
我读了很多扭曲的文档,但我仍然不确定最好的方法是什么。
有人可以举例说明如何实现这一点吗?
最佳答案
以下是演示 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:])
关于python - 异步 DNS 解析器测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15790165/