Reputation: 409
how do we set the source IP-address when we do __socket.connect((host, port))
on a machine that have a several ethernet interfaces?
Upvotes: 6
Views: 20458
Reputation: 101
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.
Upvotes: 10
Reputation: 743
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
Upvotes: 0