Python 网络时间戳(SO_TIMESTAMP、SO_TIMESTAMPNS、SO_TIMESTAMPING)

标签 python linux python-3.x networking linux-kernel

Linux 内核提供了几种方法来获取接收的(SO_TIMESTAMP、SO_TIMESTAMPNS、SO_TIMESTAMPING)或发送的(SO_TIMESTAMPING)数据包的时间戳。

内核文档:https://www.kernel.org/doc/Documentation/networking/timestamping.txt

有什么方法可以将它与 Python 一起使用吗?我在 Python 源代码中没有看到任何 SO_TIMESTAMP 常量。尝试了 3.6.2 和 GitHub master 分支。

现在,我只能使用 SIOCGSTAMP,它为我提供了上次接收到的数据包的时间戳,而发送数据包的时间戳似乎没有任何可用信息。

最佳答案

最后,我已经能够像这样获得 SO_TIMESTAMPNS 值:

SO_TIMESTAMPNS = 35
s = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.htons(3))
s.setsockopt(socket.SOL_SOCKET, SO_TIMESTAMPNS, 1)
raw_data, ancdata, flags, address = s.recvmsg(65535, 1024)

ancdata[0][2] 是作为 timespec(ulong, ulong) 的硬件时间戳。

可以在 Linux 上运行,但不能在 Mac OS X 上运行。未在 Windows 上测试。

关于Python 网络时间戳(SO_TIMESTAMP、SO_TIMESTAMPNS、SO_TIMESTAMPING),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46302308/

相关文章:

python - pandas groupby 中的行联合

python - 打包多个Python文件: Getting Import error

python - 如何使用协程作为 pytest fixture ?

Linux - Bash - 在许多用户目录中应用 chown

xml - 在 shell 脚本中需要帮助以查找和替换 xml 文件中的值

Python - 将附加的 "members"传递给函数时附加到 JSON 对象

python 象形文字

c - fprintf,错误 : format not a string literal and no format arguments [-Werror=format-security

python 3 : EOF when reading a line (Sublime Text 2 is angry)

python - 如何将 urllib.request 添加到 Herokurequirements.txt 文件?