determine IP

Keir Lawson keir at thelawsons.plus.com
Tue Nov 9 08:56:54 EST 2004


well on machines i tested, it returns the local IP. this method works
swell under the right conditions, but i dont know where my prog is going
to be run, perhaps it will be over a LAN without internet access (so it
wont be able to get to google) - is there a more robust solution?

thanks for your help so far

Keir Lawson

On Sun, 2004-11-07 at 11:58 -0500, Ed Leafe 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.
> 
>       ___/
>      /
>     __/
>    /
>   ____/
>   Ed Leafe
>   http://leafe.com/
>   http://dabodev.com/
> 
-- 




More information about the Python-list mailing list