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

11,330

Solution 1

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.

Solution 2

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

Share:
11,330
meta_warrior
Author by

meta_warrior

Updated on June 04, 2022

Comments

  • meta_warrior
    meta_warrior almost 2 years

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