[CentralOH] Local IP Address in Python

Kartic Krishnamurthy kartic.krish at gmail.com
Wed May 18 16:27:43 CEST 2011


There is the netifaces module that does exactly what you want.

In [1]: import netifaces

In [2]: netifaces.interfaces()
Out[2]: ['lo0', 'gif0', 'stf0', 'en0', 'en1', 'fw0']

In [3]: for iface in netifaces.interfaces():
   ...:     print netifaces.ifaddresses(iface)
   ...:
   ...:
{18: [{'addr': ''}], 2: [{'peer': '127.0.0.1', 'netmask': '255.0.0.0',
'addr': '127.0.0.1'}], 30: [{'peer': '::1', 'netmask':
'ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff', 'addr': '::1'}, {'netmask':
'ffff:ffff:ffff:ffff::', 'addr': 'fe80::1%lo0'}]}
.... (output truncated)

If you want to manipulate IP addresses, there are a bunch of other
libraries listed at
http://code.google.com/p/netaddr/wiki/YetAnotherPythonIPModule

+Kartic

On Wed, May 18, 2011 at 5:08 AM, Mark Erbaugh <mark at microenh.com> wrote:
> Is there a Python library function to determine the IP address or addresses of the local machine?  I'm looking for something that provides the information similar to what is displayed when you run ifconfig (Posix) or ipconfig (Windows)?
>
> Thanks,
> Mark
>
> Yes, I realize I could run ip / ifconfig in a shell, but I'm looking for something else.


More information about the CentralOH mailing list