在Python中获取传入UDP数据包的TTL

发布于 2025-01-04 08:10:56 字数 210 浏览 1 评论 0原文

我正在使用 Python 中的套接字模块进行一些基本的 UDP 客户端-服务器通信。我需要做的非常简单:客户端向服务器发送一个数据包,服务器用客户端的公共 IP 地址、端口和代表 UDP 数据包到达服务器时的 TTL 的数字进行应答。这是我的主要问题:有什么方法可以使用recvfrom() 等接收数据包,并读取它到达我的服务器时的 TTL 值吗?

非常感谢!

马泰奥·蒙蒂

I'm using the socket module in Python to do some basic UDP client-server communication. What I would need to do is quite simple: client sends server a packet, server answers with client's public ip address, port and a number representing the TTL the UDP packet had when it got to the server. This is my main problem: is there any way to recieve a packet with recvfrom() or so, and read the TTL value it had when it reached my server?

Thank you very much!

Matteo Monti

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

难理解 2025-01-11 08:10:56

I think you want to setsockopt(IP_RECVTTL) and then use recvmsg(). But Python doesn't seem to have recvmsg in its standard libraries (see http://bugs.python.org/issue6560). So probably you will need to write a small C or C++ shared library which is importable by Python and which does what you want. Or maybe try using the patches from page I linked.

嘦怹 2025-01-11 08:10:56

Python 3.51 支持 IP_RECVTTL 或 IP_RECVTOS 等标志。我尝试了一下,它在 3.x Linux 内核中对我有用。

Python 3.51 has the support for flags such as IP_RECVTTL or IP_RECVTOS. I gave it a try and it worked for me in a 3.x linux kernel.

夏の忆 2025-01-11 08:10:56

据我所知,这通常不会暴露给用户空间。我认为您必须使用 libpcap 之类的东西来完成此操作,如 Stack Overflow 答案中所述:

https://stackoverflow.com /a/2362554/864393

This isn't generally exposed to userspace, as far as I know. I think you'll have to use something like libpcap to accomplish this, as described in this Stack Overflow answer:

https://stackoverflow.com/a/2362554/864393

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文