473,390 Members | 1,482 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,390 software developers and data experts.

Clean way to get one's network IP address?

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
13 3449
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
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
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
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
What would be a good way to do this?

using 'netifaces'?
see http://pypi.python.org/pypi/netifaces/0.3
Nov 21 '07 #6
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
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
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
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
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
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
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Grant Richard | last post by:
Using the TcpListener and TcpClient I created a program that just sends and receives a short string - over and over again. The program is fine until it gets to around 1500 to 1800 messages. At...
6
by: Tom Kent | last post by:
I have a computer with two network cards (attached to seperate networks) and I am having trouble getting my C# application to send its multicast packets on a specific one. It receives just fine...
2
by: Ronald | last post by:
I just started with dotnetnuke, and with a wrong login (wrong password on a clean install i can crash the application pool from IIS. (when i login with the right username/password information the...
8
by: rongchaua | last post by:
Hi all, i would like now to disable and enable network adapter programmatically with c#. I have searched but found nothing useful. There's no topic about this problem. Has someone done with this...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.