Clean way to get one's network IP address?

MonkeeSage MonkeeSage at gmail.com
Fri Nov 23 22:29:58 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.

For linux (and possibly other POSIX-like OS', though reported not to
work on FreeBSD), see the pure python 'ifconfig' script I posted here:

http://groups.google.com/group/comp.lang.python/browse_thread/thread/52ad421ed64ec3fc/13e2a0609920c27b?lnk=gst&q=monkeesage+hwaddr#a4419fd2c52078e2

It uses low-level ioctl to query the same values as are displayed in
ifconfig. It's obviously not very portable, but I didn't know what
exactly you meant by "independent".

Regards,
Jordan




More information about the Python-list mailing list