meta_warrior
meta_warrior

Reputation: 409

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

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

Answers (2)

WillJ
WillJ

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

Raskayu
Raskayu

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

Related Questions