By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,035 Members | 1,995 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,035 IT Pros & Developers. It's quick & easy.

Clean way to get one's network IP address?

P: n/a
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.
Nov 21 '07 #1
Share this Question
Share on Google+
13 Replies


P: n/a
On Nov 21, 9:15 am, Gilles Ganault <nos...@nospam.comwrote:
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.
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
Nov 21 '07 #2

P: n/a
On Wed, 21 Nov 2007 07:20:45 -0800 (PST), Paul McGuire
<pt***@austin.rr.comwrote:
>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?
Nov 21 '07 #3

P: n/a
Gilles Ganault <no****@nospam.comwrote:
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?
Nov 21 '07 #4

P: n/a
On Nov 21, 2007 10:15 AM, Gilles Ganault <no****@nospam.comwrote:
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'
>>>
Nov 21 '07 #5

P: n/a
What would be a good way to do this?

using 'netifaces'?
see http://pypi.python.org/pypi/netifaces/0.3
Nov 21 '07 #6

P: n/a
On Wed, 21 Nov 2007 12:00:52 -0500, Joe Riopel wrote:
On Nov 21, 2007 10:15 AM, Gilles Ganault <no****@nospam.comwrote:
>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?

--
Steven
who seems to be making a lot of snarky comments lately.
Nov 21 '07 #7

P: n/a
Gilles Ganault <no****@nospam.comwrites:
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?
Here's one way:

http://www.google.com/codesearch?hl=...onfig.py#first
John
Nov 22 '07 #8

P: n/a
On Nov 21, 5:34 pm, Steven D'Aprano <st...@REMOVE-THIS-
cybersource.com.auwrote:
On Wed, 21 Nov 2007 12:00:52 -0500, Joe Riopel wrote:
On Nov 21, 2007 10:15 AM, Gilles Ganault <nos...@nospam.comwrote:
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.
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?
Nov 23 '07 #9

P: n/a
On Fri, 23 Nov 2007 08:52:58 -0800, dr******@gmail.com wrote:
On Nov 21, 5:34 pm, Steven D'Aprano <st...@REMOVE-THIS-
cybersource.com.auwrote:
>On Wed, 21 Nov 2007 12:00:52 -0500, Joe Riopel wrote:
On Nov 21, 2007 10:15 AM, Gilles Ganault <nos...@nospam.comwrote:
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.
Nov 23 '07 #10

P: n/a
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.audeclaimed 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.
Nov 24 '07 #11

P: n/a
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.
Nov 24 '07 #12

P: n/a
On Nov 21, 9:15 am, Gilles Ganault <nos...@nospam.comwrote:
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.
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....419fd2c52078e2

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

Nov 24 '07 #13

P: n/a
"Steven D'Aprano" <ste..HIS-cybersource.com.auwrote:

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".
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

Nov 24 '07 #14

This discussion thread is closed

Replies have been disabled for this discussion.