python - 异步 DNS 解析器测试

标签 python twisted

我想测试大量 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/

相关文章:

python - 使用 Climin 在 Theano 中实现逻辑回归

python - 从 csv 文件 : Error IO operation on closed file 中获取数据

redis - 仅当我使用异步数据库操作时,Twisted 才不会发回数据

python - 在 Python Twisted 线程中使用 Cmd 模块

python - Scrapy Pyinstaller OSError : could not get source code/twisted. internet.defer._DefGen_Return

python - 如何在numpy中优化此图像迭代?

python - 计算Python中数据框的系列类型列中字符串的出现次数

python - twistd.py 记录到标准输出和文件

python - 比较多处理与扭曲的问题

python - 为什么 numpy random.choice() 函数被停用了?