How set the source IP adress

Grant Edwards grante at visi.com
Fri Oct 27 14:25:41 EDT 2006


On 2006-10-27, Grant Edwards <grante at visi.com> wrote:
> 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))

Doh!  That should have been 0 not -1.

-- 
Grant Edwards                   grante             Yow!  Do I hear th'
                                  at               SPINNING of various
                               visi.com            WHIRRING, ROUND, and WARM
                                                   WHIRLOMATICS?!



More information about the Python-list mailing list