vicesbur
vicesbur

Reputation: 333

UDP packets with python

I have a mobile router that can be configured by using different Python script. What I need to do is read all the packets arriving to the router in a concrete UDP port to copy this information in a .txt file afterwards.

Anyone can give me some tips about how can I do this using Python? How can I detect every time a packet arrives in to the router?

Thank you.

Upvotes: 1

Views: 468

Answers (1)

Raymond Hettinger
Raymond Hettinger

Reputation: 226256

Here's a quick example of how to bind to a UDP port and do some action whenever a datagram is received:

import socket

s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind(('', 9800))
try:
    while True:
        result, who = s.recvfrom(256)
        print result, who
finally:
    s.close()

Upvotes: 2

Related Questions