How set the source IP adress

M

Maksim Kasimov

Hi,

how to set source ip-address when do __socket.connect((host, port))
on a machine that have a several ip-adresses?

many thanks for advice.

__socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
__socket.connect((host, port))
 
I

Irmen de Jong

Maksim said:
Hi,

how to set source ip-address when do __socket.connect((host, port))
on a machine that have a several ip-adresses?

many thanks for advice.

__socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
__socket.connect((host, port))

sock.connect ( ('11.22.33.44', 9999) )

i.e. just put the ip address as string parameter into the connect address tuple
(where you wrote: host)

--Irmen
 
G

Grant Edwards

sock.connect ( ('11.22.33.44', 9999) )

i.e. just put the ip address as string parameter into the
connect address tuple (where you wrote: host)

Um, no. That controls the _destination_ IP address, not the
source. If you want to control the source IP addresss you can
bind it to a local interface's IP address before doing the
connect() call.

sock.bind(('10.0.0.99',-1)) # -1: don't care about source port number
sock.connect((host,port))

That will make sure that 10.0.0.99 is used as the source IP
address. Of course you have to have an interface with that
address.
 
G

Grant Edwards

Um, no. That controls the _destination_ IP address, not the
source. If you want to control the source IP addresss you can
bind it to a local interface's IP address before doing the
connect() call.

sock.bind(('10.0.0.99',-1)) # -1: don't care about source port number
sock.connect((host,port))

Doh! That should have been 0 not -1.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top