Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 测量DNS解析时间的最佳方法_Python 3.x_Dns_Dnspython - Fatal编程技术网

Python 3.x 测量DNS解析时间的最佳方法

Python 3.x 测量DNS解析时间的最佳方法,python-3.x,dns,dnspython,Python 3.x,Dns,Dnspython,我正在使用dns python编写一个基于python 3.6的脚本来监控dns解析时间。我需要了解获得解决时间的最佳方法。我目前正在使用以下工具: 方法:1 import dns.resolver import time dns_start = time.perf_counter() answers = dns.resolver.query(domain, qtype) dns_end = time.perf_counter() print("DNS record is: " ,answer

我正在使用dns python编写一个基于python 3.6的脚本来监控dns解析时间。我需要了解获得解决时间的最佳方法。我目前正在使用以下工具:

方法:1

import dns.resolver
import time

dns_start = time.perf_counter()
answers = dns.resolver.query(domain, qtype)
dns_end = time.perf_counter()
print("DNS record is: " ,answers.rrset)
print('DNS time =' ,(dns_end - dns_start)* 1000,"ms")
方法2

import dns.resolver
import time

answers = dns.resolver.query(domain, qtype)
print("DNS record is: " ,answers.rrset)
print('DNS time =' , answers.response.time * 1000,"ms")
谢谢

正如那句名言所说的“视情况而定”

您需要在“最佳方法”中定义什么是“最佳”。这两种方法做的都是正确的,只是不同而已

首先计算网络时间、远程服务器处理时间,然后计算DNS消息的本地生成和解析时间,包括Python在dnspython库中花费的时间

第二种方法只考虑网络和远程处理时间:如果您在中搜索
响应时间
,您将看到它被计算为发送消息之前(因此在构建消息之后,因此不包括生成时间)和接收消息之后的时间差(因此在解析之前,不包括解析时间)

第二种方法基本上可以测试远程服务器的性能,而不考虑本地程序(包括Python本身和dnspython库)的性能(但您和远程服务器之间所需的网络时间将计入)

第一个方法显示感知的时间,因为它包括代码中执行某些操作所需的所有时间,即构建DNS数据包并解析结果,没有这些操作,调用应用程序无法处理DNS内容