Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Clean way to get one's network IP address?

216 views
Skip to first unread message

Gilles Ganault

unread,
Nov 21, 2007, 10:15:43 AM11/21/07
to
Hello

I need to get the local computer's IP address, ie. what's displayed
when running "ifconfig" in Linux:

# ifconfig
eth0 Link encap:Ethernet HWaddr 00:15:58:A1:D5:6F
inet addr:192.168.0.79 Bcast:192.168.0.255
Mask:255.255.255.0

I know about socket.gethostbyname, but this relies on what's in
/etc/hosts, and I'd rather have a more independent solution.

What would be a good way to do this?

Thank you.

Paul McGuire

unread,
Nov 21, 2007, 10:20:45 AM11/21/07
to

Be aware that it is very possible to have multiple IP addresses from
which to choose (systems have multiple network adapters, VPNs,
wireless, etc.), so the question might not be how to get THE IP
address, but how to get THE RIGHT IP address. Selecting the right IP
address from among several is not always clear cut.

-- Paul

Gilles Ganault

unread,
Nov 21, 2007, 10:31:35 AM11/21/07
to
On Wed, 21 Nov 2007 07:20:45 -0800 (PST), Paul McGuire
<pt...@austin.rr.com> wrote:
>Be aware that it is very possible to have multiple IP addresses from
>which to choose

Yup, but this is the local host, and it only has a single interface.
Should I look into os.environ() or something like that?

Roy Smith

unread,
Nov 21, 2007, 11:39:57 AM11/21/07
to
Gilles Ganault <nos...@nospam.com> wrote:

> I know about socket.gethostbyname, but this relies on what's in
> /etc/hosts, and I'd rather have a more independent solution.

The system I'm currently working on uses exactly this strategy -- we get
the hostname then do a name lookup on it. We've gone around and around on
this, and ended up with that being the best solution. For us, anyway.
Your mileage may vary.

As others have pointed out, it's entirely possible to have multiple IP
addresses. In addition, your IP address(es) can change as connections come
up and down, especially in a mobile environment (WiFi, VPN, cellular, etc).
There is no single correct answer here.

Oh, BTW, did you mean IPv4 or IPv6?

Jean-Paul Calderone

unread,
Nov 21, 2007, 11:47:09 AM11/21/07
to pytho...@python.org

You can generally get the local address which will be used for traffic
to a particular host by getting ready to send traffic to it with a UDP
socket:

>>> from socket import socket, SOCK_DGRAM, AF_INET
>>> s = socket(AF_INET, SOCK_DGRAM)
>>> s.connect(('google.com', 0))
>>> s.getsockname()
('192.168.1.113', 43711)

Jean-Paul

Joe Riopel

unread,
Nov 21, 2007, 12:00:52 PM11/21/07
to Gilles Ganault, pytho...@python.org
On Nov 21, 2007 10:15 AM, Gilles Ganault <nos...@nospam.com> wrote:
> I know about socket.gethostbyname, but this relies on what's in
> /etc/hosts, and I'd rather have a more independent solution.

I might be missing something in your question, but on a Windows XP
machine, I can get the IP address of my machine using:
>>> from socket import gethostname, gethostbyname
>>> gethostbyname(gethostname())
'192.168.0.11'
>>>

poltrone

unread,
Nov 21, 2007, 2:58:32 PM11/21/07
to
> What would be a good way to do this?

using 'netifaces'?
see http://pypi.python.org/pypi/netifaces/0.3


Steven D'Aprano

unread,
Nov 21, 2007, 5:34:16 PM11/21/07
to


Just out of curiosity, what part of the Original Poster's comment that he
already knew about socket.gethostbyname did you not understand?

--
Steven
who seems to be making a lot of snarky comments lately.

John J. Lee

unread,
Nov 22, 2007, 4:50:06 PM11/22/07
to

drob...@gmail.com

unread,
Nov 23, 2007, 11:52:58 AM11/23/07
to
On Nov 21, 5:34 pm, Steven D'Aprano <st...@REMOVE-THIS-
I'm not sure if an answer is wanted here, or if the poster just enjoys
being a jerk.
The original poster was concerned that socket.gethostbyname
read the /etc/hosts file. I have no idea why that should be a
problem but apparently it was.
Joe Riopel pointed out that it works on Windows XP.
That's great news since I'm running XP myself. If I ever
need to find out my IP address, and I have no idea why
I would, it looks like that's how to do it. Thanks, Joe.
Now, my copy of Windows XP does not have a file
called /etc/hosts. Perhaps the original poster should
upgrade to Windows XP. Or maybe he just needs to
delete /etc/hosts. Not sure what that would do.
In any case, if on some system socket.gethostbyname
doesn't work, shouldn't it be fixed?

Steven D'Aprano

unread,
Nov 23, 2007, 5:56:41 PM11/23/07
to
On Fri, 23 Nov 2007 08:52:58 -0800, drob...@gmail.com wrote:

> On Nov 21, 5:34 pm, Steven D'Aprano <st...@REMOVE-THIS-
> cybersource.com.au> wrote:
>> On Wed, 21 Nov 2007 12:00:52 -0500, Joe Riopel wrote:
>> > On Nov 21, 2007 10:15 AM, Gilles Ganault <nos...@nospam.com> wrote:
>> >> I know about socket.gethostbyname, but this relies on what's in
>> >> /etc/hosts, and I'd rather have a more independent solution.
>>
>> > I might be missing something in your question, but on a Windows XP
>> > machine, I can get the IP address of my machine using:
>> >>>> from socket import gethostname, gethostbyname
>> >>>> gethostbyname(gethostname())
>> > '192.168.0.11'
>>
>> Just out of curiosity, what part of the Original Poster's comment that
>> he already knew about socket.gethostbyname did you not understand?
> I'm not sure if an answer is wanted here, or if the poster just enjoys
> being a jerk.
> The original poster was concerned that socket.gethostbyname
> read the /etc/hosts file. I have no idea why that should be a problem
> but apparently it was.

Frankly, neither do I.

> Joe Riopel pointed out that it works on Windows XP.

Yes, it does. Most things in Python are platform independent.


> That's great news since I'm running XP myself. If I ever need to find
> out my IP address, and I have no idea why I would, it looks like that's
> how to do it. Thanks, Joe.
> Now, my copy of Windows XP does not have a file called /etc/hosts.

I'm not surprised. Windows XP usually stores the hosts file here:

C:\system32\drivers\etc\hosts

> Perhaps the original poster should upgrade to Windows XP.

Oh my, that's one of the funniest things I've ever read. You are joking,
right?


> Or maybe he just needs to delete /etc/hosts. Not sure what that
> would do.

Almost certainly it would seriously break lots of things. Besides, he may
not have root access on the machine he's running Python on.

> In any case, if on some system socket.gethostbyname
> doesn't work, shouldn't it be fixed?

Who says it doesn't work?

--
Steven.

Message has been deleted

Steven D'Aprano

unread,
Nov 23, 2007, 7:05:13 PM11/23/07
to
On Fri, 23 Nov 2007 15:58:06 -0800, Dennis Lee Bieber wrote:

> On Fri, 23 Nov 2007 22:56:41 -0000, Steven D'Aprano
> <st...@REMOVE-THIS-cybersource.com.au> declaimed the following in
> comp.lang.python:


>
>
>> I'm not surprised. Windows XP usually stores the hosts file here:
>>
>> C:\system32\drivers\etc\hosts
>>
>>

> Pardon...
>
> My system shows:
>
> C:\I386\HOSTS
> C:\WINDOWS\SYSTEM32\DRIVERS\ETC\HOSTS


I don't know about the i386, but you're absolutely write, I forgot the
Windows directory. Oops.


--
Steven.

Steven D'Aprano

unread,
Nov 23, 2007, 7:13:40 PM11/23/07
to
On Sat, 24 Nov 2007 00:05:13 +0000, Steven D'Aprano wrote:

> ... you're absolutely write ...

Okay, I now officially have no more credibility left. Time for me to get
a Hotmail email address and open a MySpace page and spend all my time
writing "OMG LOL LOL LOL did u c teh thing on Ausrtalia Idle lastnight
lol lol lol it was soooooo much omg".


--
Steven.

Message has been deleted

MonkeeSage

unread,
Nov 23, 2007, 10:29:58 PM11/23/07
to
On Nov 21, 9:15 am, Gilles Ganault <nos...@nospam.com> wrote:

For linux (and possibly other POSIX-like OS', though reported not to
work on FreeBSD), see the pure python 'ifconfig' script I posted here:

http://groups.google.com/group/comp.lang.python/browse_thread/thread/52ad421ed64ec3fc/13e2a0609920c27b?lnk=gst&q=monkeesage+hwaddr#a4419fd2c52078e2

It uses low-level ioctl to query the same values as are displayed in
ifconfig. It's obviously not very portable, but I didn't know what
exactly you meant by "independent".

Regards,
Jordan

Hendrik van Rooyen

unread,
Nov 24, 2007, 12:58:02 AM11/24/07
to pytho...@python.org

That is not too bad - for real time typing by a male.
Chat room parlance is not the same as what is supposed to be
considered posting...

homophones ruul, OK?

rotflmfao

- Hendrik

0 new messages