Clean way to get one's network IP address?

Paul McGuire ptmcg at austin.rr.com
Wed Nov 21 10:20:45 EST 2007


On Nov 21, 9:15 am, Gilles Ganault <nos... at nospam.com> wrote:
> Hello
>
> I need to get the local computer's IP address, ie. what's displayed
> when running "ifconfig" in Linux:
>
> # ifconfig
> eth0      Link encap:Ethernet  HWaddr 00:15:58:A1:D5:6F  
>           inet addr:192.168.0.79  Bcast:192.168.0.255
> Mask:255.255.255.0
>
> I know about socket.gethostbyname, but this relies on what's in
> /etc/hosts, and I'd rather have a more independent solution.
>
> What would be a good way to do this?
>
> Thank you.

Be aware that it is very possible to have multiple IP addresses from
which to choose (systems have multiple network adapters, VPNs,
wireless, etc.), so the question might not be how to get THE IP
address, but how to get THE RIGHT IP address.  Selecting the right IP
address from among several is not always clear cut.

-- Paul



More information about the Python-list mailing list