How to get all IP addresses in python?

Yun Mao maoy_REMOVE_IF_NOT_SPAM at cis.upenn.edu
Fri Jul 11 20:50:00 CEST 2003


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" <spinard at ra.rockwell.com> wrote in message
news:6cd58b6.0307110715.1bb9bc23 at posting.google.com...
> 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






More information about the Python-list mailing list