How to get all IP addresses in python?

David Bolen db3l at fitlinxx.com
Mon Jul 14 20:20:57 CEST 2003


Afanasiy <abelikov72 at 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





More information about the Python-list mailing list