how do we set the source IP-address when we do __socket.connect((host, port))
on a machine that have a several ethernet interfaces?
specifying source IP-address for socket.connect() in python sockets
Before you use connect()
, use
socket.bind((ipaddr, port))
to determine the source addr and source port. If addr or port is equal to '' or 0, it means using OS default.
Just set the host IP like @Jalo said connect(('179.XX.XX.XX', 5005))
, the system will choose wich interface needs to use to interact with that host.
If you need more info to understand how just read Routing in Linux
I interpret the question as being specifically about overriding the default routing. How to open a socket using the default routing is not a very relevant answer. –
Bucky
© 2022 - 2024 — McMap. All rights reserved.
connect(('179.XX.XX.XX', 5005))
is enough for setting the connection – Micco