Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: How to get all IP addresses in python?

Thread Tools

Re: How to get all IP addresses in python?

Steve Pinard
Posts: n/a
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
Reply With Quote
David Bolen
Posts: n/a
Afanasiy <(E-Mail Removed)> 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

Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
How to implement a firewall for Windows platform that blocks based on Mac addresses instead of IP addresses C Programming 1 12-07-2006 04:30 AM
Physical Addresses VS. Logical Addresses namespace1 C++ 3 11-29-2006 03:07 PM
Get list of all IP addresses svirdi C++ 2 08-08-2006 05:18 PM
PIX 501-Closing SMTP to all inside addresses except Server Mac Hammer Cisco 5 06-21-2005 12:09 PM