determine IP

exarkun at divmod.com exarkun at divmod.com
Sun Nov 7 12:38:46 EST 2004


On Sun, 7 Nov 2004 11:58:21 -0500, Ed Leafe <ed at leafe.com> wrote:
>On Nov 7, 2004, at 11:53 AM, exarkun at divmod.com wrote:
> 
> >   The above code will _always_ return the local IP address, regardless 
> > of NAT configuration.  For example, running on my desktop:
> >
> >>>> import socket
> >>>> s = socket.socket()
> >>>> s.connect(('google.com', 80))
> >>>> print s.getsockname()
> >     ('192.168.42.4', 40044)
> >>>>
> >>>>
> 
> 	Not OMM:
> 
>  >>> import socket
>  >>> s = socket.socket()
>  >>> s.connect(('google.com', 80))
>  >>> s.getsockname()
> ('66.66.222.137', 55096)
>  >>>
> 
> 	The local IP address is in the 192.168 subnet.

  It sounds like you are running this code on the machine performing the NAT'ing.  It's "local" address can easily be argued to be either of those two addresses, since it sits on two networks.  The behavior I referred to was for any of the other machines on the NAT'd network.

  Jp



More information about the Python-list mailing list