Python ntplib响应中的所有字段是什么?它们是如何使用的?

问题描述 投票:0回答:1

我想测量本地时钟和运行NTP服务器的远程处理器之间的区别。

我可以获得响应的tx_time,如下所示,但更好的估计将包括对网络延迟的一些估计。 NTP响应消息中还有其他字段也应该使用。

import ntplib
from time import ctime,time

addr_remote = '128.128.204.207'

c = ntplib.NTPClient()
remote = c.request(addr_remote)
local = time()
print("REMOTE: " + ctime(remote.tx_time) + "   <reference clock>   ") 
print("LOCAL:  " + ctime(local)        + "   delta: " + str(local - remote.tx_time ))

如果我看“远程”:

for attr in dir(remote):
    print("remote.%s = %r" % (attr, getattr(remote, attr)))

我知道了:

remote.delay = 0.0
remote.dest_time = 1531863145.9309998
remote.dest_timestamp = 3740851945.9309998
remote.from_data = <bound method NTPPacket.from_data of <ntplib.NTPStats object at 0x000000000265B2E8>>
remote.leap = 0
remote.mode = 4
remote.offset = -1.8789582252502441
remote.orig_time = 1531863145.9309998
remote.orig_timestamp = 3740851945.9309998
remote.poll = 0
remote.precision = -7
remote.recv_time = 1531863144.0520415
remote.recv_timestamp = 3740851944.0520415
remote.ref_id = 0
remote.ref_time = 0.0
remote.ref_timestamp = 2208988800.0
remote.root_delay = 0.0
remote.root_dispersion = 0.0
remote.stratum = 9
remote.to_data = <bound method NTPPacket.to_data of <ntplib.NTPStats object at 0x000000000265B2E8>>
remote.tx_time = 1531863144.0520415
remote.tx_timestamp = 3740851944.0520415

那么,我该如何使用这些:

  • dest_time
  • orig_time
  • recv_time
  • tx_time

消除网络延迟,并更好地估计时钟差异?

python ntp
1个回答
2
投票

NTP客户端发送一个包含本地时间orig_time的数据包,NTP服务器在服务器时间recv_time收到该数据包。然后服务器在服务器时间tx_time回复,客户端在当地时间dest_time收到该回复。

往返delay计算为recv_time - orig_time + dest_time - tx_time,时钟之间的偏移量为offset = (recv_time - orig_time + tx_time - dest_time) / 2

假设两个NTP数据包采用一致的路径,正确的调整时间就是dest_time + offset,相当于tx_time + delay/2

© www.soinside.com 2019 - 2024. All rights reserved.