shivram shivram - 1 year ago 196
Python Question

Python : Receive UDP packets from port

I have a client which is creating packets and sending packets to a destination in a network which has been created using mininet. Now I am writing a python program at the destination to count the number of packets which has arrived. Now I know for sure that the packets are arriving at the destination (used tcpdump to verify it)

How do I go about it?

I thought of using this -

s = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_TCP)
print s.recvfrom(5001)

But this seems to be slow. Is there any other alternative?

Answer Source

You want socket.IPPROTO_UDP for UDP packets, but otherwise, that's basically what you must do. No matter what other things you try, it's going to have to do those things.

Oh, and you'll want to do a socket.bind(('',PORT)) to bind it to the port you want it to listen on.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download