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

Mat*_*att 6 python ntp

我想测量本地时钟和运行 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 ))
Run Code Online (Sandbox Code Playgroud)

如果我看“远程”:

for attr in dir(remote):
    print("remote.%s = %r" % (attr, getattr(remote, attr)))
Run Code Online (Sandbox Code Playgroud)

我懂了:

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
Run Code Online (Sandbox Code Playgroud)

那么,我如何使用这些:

  • 目的地时间
  • 原点时间
  • 接收时间
  • 时间

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

Vic*_*Vic 8

NTP 客户端发送带有本地时间的数据包,orig_timeNTP 服务器在服务器时间接收该数据包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