How set the source IP adress

Grant Edwards grante at
Fri Oct 27 20:25:41 CEST 2006

On 2006-10-27, Grant Edwards <grante at> wrote:
> On 2006-10-27, Irmen de Jong <irmen.NOSPAM at> 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 ( ('', 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(('',-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
                                 WHIRRING, ROUND, and WARM

More information about the Python-list mailing list