16

I'd like to send a specific UDP broadcast packet. Unfortunately, I need to send the UDP packets from a very specific port.

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

My broadcast socket setup looks like this:

s = socket(AF_INET,SOCK_DGRAM)

s.setsockopt(SOL_SOCKET, SO_BROADCAST, 1)

How can I then set the source port in Python?

2 Answers 2

22

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))
Sign up to request clarification or add additional context in comments.

Comments

12

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.