specifying source IP-address for socket.connect() in python sockets
Asked Answered
C

2

5

how do we set the source IP-address when we do __socket.connect((host, port)) on a machine that have a several ethernet interfaces?

Cierracig answered 5/12, 2016 at 9:32 Comment(1)
Explain a bit more the question. Using connect(('179.XX.XX.XX', 5005)) is enough for setting the connectionMicco
K
9

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.

Kowalewski answered 12/3, 2017 at 20:14 Comment(0)
C
0

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

Centrum answered 5/12, 2016 at 9:43 Comment(1)
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.