How set the source IP adress

Grant Edwards grante at visi.com
Fri Oct 27 14:23:53 EDT 2006


On 2006-10-27, Irmen de Jong <irmen.NOSPAM at xs4all.nl> wrote:

>> how to set source ip-address when do __socket.connect((host, port))
>> on a machine that have a several ip-adresses?
>> 
>> __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)

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.

-- 
Grant Edwards                   grante             Yow!  I'm a nuclear
                                  at               submarine under the
                               visi.com            polar ice cap and I need
                                                   a Kleenex!



More information about the Python-list mailing list