Velocity Reviews

Velocity Reviews (
-   Python (
-   -   Re: How to get all IP addresses in python? (

Steve Pinard 07-11-2003 03:15 PM

Re: How to get all IP addresses in python?
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

David Bolen 07-14-2003 06:20 PM

Re: How to get all IP addresses in python?
Afanasiy <> writes:

> FYI, It works perfectly on my Windows 2000 machine.
> ... import socket
> ... print socket.getaddrinfo(socket.gethostname(), None)
> [(2, 0, 0, '', ('', 0)), (2, 0, 0, '', ('',
> 0)), (2, 0, 0, '', ('', 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

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

-- David

All times are GMT. The time now is 05:56 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.