Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Re: How to get all IP addresses in python? (http://www.velocityreviews.com/forums/t319555-re-how-to-get-all-ip-addresses-in-python.html)

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 <abelikov72@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



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.