views:

307

answers:

2

Hi all,

I'd like to send a specific UDP broadcast packet.. unfortunatly i need to send the udp packet from a very specific port for all packet I send.

Let say I broadcast via UDP "BLABLAH", the server will only answer if my incoming packet source port was 1444, if not the packet is discarded.

My broadcast socket setup look like this :

s = socket(AF_INET,SOCK_DGRAM)

s.setsockopt(SOL_SOCKET, SO_BROADCAST, 1)

How can i do that (set the source port) in python ?

Thanks!

+2  A: 

You need to bind the socket to the specific port you want to send from. The bind method takes an address tuple, much like connect, though you can use the wildcard address. For example:

s.bind(('0.0.0.0', 1444))
Shtééf
+1  A: 

Use s.bind(('', port)).

jweyrich