在Python中获取传入UDP数据包的TTL
我正在使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我想你想setsockopt(IP_RECVTTL)然后使用recvmsg()。但是Python的标准库中似乎没有recvmsg(参见http://bugs.python.org/issue6560< /a>)。因此,您可能需要编写一个小型 C 或 C++ 共享库,该库可由 Python 导入并执行您想要的操作。或者尝试使用我链接的页面中的补丁。
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.
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.
据我所知,这通常不会暴露给用户空间。我认为您必须使用 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