Clean way to get one's network IP address?

G

Gilles Ganault

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

Paul McGuire

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
 
G

Gilles Ganault

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?
 
R

Roy Smith

Gilles Ganault said:
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?
 
J

Joe Riopel

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:
 
S

Steven D'Aprano

I might be missing something in your question, but on a Windows XP
machine, I can get the IP address of my machine using:


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

John J. Lee

Gilles Ganault said:
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...site-packages/linkcheck/dns/ifconfig.py#first


John
 
D

drobinow

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?
 
S

Steven D'Aprano

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?
 
S

Steven D'Aprano

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

Steven D'Aprano

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

Dennis Lee Bieber

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

\I386 is one of those cryptic directories M$ created... As I recall,
it contains a supposed backup of all the system critical files (for the
x86 32-bit version) and the system does comparisons against the versions
elsewhere... Patch releases have to do some game to update both or the
I386 version will over-write the other...
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
M

MonkeeSage

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...?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
 
H

Hendrik van Rooyen

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top