问题
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
回答1:
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.
回答2:
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.
回答3:
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
来源:https://stackoverflow.com/questions/9237006/getting-ttl-of-incoming-udp-packet-in-python