Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to get all IP addresses in python?

6,304 views
Skip to first unread message

Yun Mao

unread,
Jul 11, 2003, 12:54:47 AM7/11/03
to
Hi. I did a little homework on google for this question. The answer was:
>>> import socket
>>> hostname = socket.gethostname()
>>> ip = socket.gethostbyname(hostname)
>>> print ip
192.168.55.101
But what if I have several interfaces, say eth0, eth0:1, eth1, etc. How to
get all of them? It would be a little undesirable if people suggest to parse
the result of ifconfig because I in particular want the code to be cross
platform. Thanks a lot!

Yun


Roy Smith

unread,
Jul 11, 2003, 7:46:13 AM7/11/03
to

You might try using gethostname() as above to get the canonical host
name for your machine, then using one of the various Python DNS modules
to look up all the A records for that name. Not perfect, but a start.

Steve Pinard

unread,
Jul 11, 2003, 11:15:45 AM7/11/03
to
Try socket.getaddrinfo rather than socket.gethostbyname. It returns a
list of tuples. tuple[4][0] of each list element is the IP address.

addrs = socket.getaddrinfo(socket.gethostname(), None)
for addr in addrs:
print addr[4][0]

The above worked on my machine but I only have one NIC card.

- Steve

Yun Mao

unread,
Jul 11, 2003, 2:50:00 PM7/11/03
to
Sorry, this still doesn't work very well on my machine.

>>> print socket.getaddrinfo(socket.gethostname(), None)
[(2, 1, 6, '', ('127.0.0.1', 0)), (2, 2, 17, '', ('127.0.0.1', 0)), (2, 3,
0, '', ('127.0.0.1', 0))]
>>>

Any ideas? Thanks!

Yun

"Steve Pinard" <spi...@ra.rockwell.com> wrote in message
news:6cd58b6.03071...@posting.google.com...

Afanasiy

unread,
Jul 11, 2003, 8:51:33 PM7/11/03
to
On Fri, 11 Jul 2003 14:50:00 -0400, "Yun Mao"
<maoy_REMOVE...@cis.upenn.edu> wrote:

>Sorry, this still doesn't work very well on my machine.
>
>>>> print socket.getaddrinfo(socket.gethostname(), None)
>[(2, 1, 6, '', ('127.0.0.1', 0)), (2, 2, 17, '', ('127.0.0.1', 0)), (2, 3,
>0, '', ('127.0.0.1', 0))]
>>>>
>
>Any ideas? Thanks!

FYI, It works perfectly on my Windows 2000 machine.

... import socket
... print socket.getaddrinfo(socket.gethostname(), None)
[(2, 0, 0, '', ('124.181.217.203', 0)), (2, 0, 0, '', ('169.254.25.142',
0)), (2, 0, 0, '', ('169.254.218.201', 0))]
...

Good to know too!

David Bolen

unread,
Jul 14, 2003, 2:20:57 PM7/14/03
to
Afanasiy <abeli...@hotmail.com> writes:

> FYI, It works perfectly on my Windows 2000 machine.
>
> ... import socket
> ... print socket.getaddrinfo(socket.gethostname(), None)
> [(2, 0, 0, '', ('124.181.217.203', 0)), (2, 0, 0, '', ('169.254.25.142',
> 0)), (2, 0, 0, '', ('169.254.218.201', 0))]
> ...

Just remember that all of these approaches presume that your local DNS
server (or whatever server responds for your local machine name) has
all of the various possible addresses. There's no guarantee that DNS
will match precisely what your machine is actually configured with,
including perhaps not having all of your addresses. So its entirely
possible to return appropriate information in one case but not in
another.

What is really desired for this sort of query is to ask the system
about the actual configured interfaces - getting local IP addresses
shouldn't need to involve DNS at all (not to mention the traffic
necessary to resolve the information). The problem is that checking
the physical interface configuration is not a portable procedure
(specific IOCtls on most Unix systems, not positive about Windows but
I know registry querying could do it). This isn't an issue limited to
Python.

-- David

kdiego...@gmail.com

unread,
Feb 24, 2016, 8:15:05 PM2/24/16
to
import socket

def get_ipaddr():
proc = subprocess.Popen(["ip addr show | egrep inet | awk '{{print $2}}' | awk -F'/' '{{print $1}}' | egrep -v '^127' | xargs'"],stdout=subprocess.PIPE, shell=True)
x = proc.communicate()[0]
return x.strip()

MrJean1

unread,
Feb 24, 2016, 9:22:20 PM2/24/16
to

Try function getIPs from this module

<https://code.activestate.com/recipes/577191>

/Jean
0 new messages