gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 20:23:07 25 4
gpt4 key购买 nike

我想测量本地时钟和
运行 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

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

    最佳答案

    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 .

    关于python - Python ntplib 响应中的所有字段是什么,它们是如何使用的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51390551/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com