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
Yup, but this is the local host, and it only has a single interface.
Should I look into os.environ() or something like that?
> 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?
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
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'
>>>
using 'netifaces'?
see http://pypi.python.org/pypi/netifaces/0.3
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.
> 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.
> 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.
> ... 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.
For linux (and possibly other POSIX-like OS', though reported not to
work on FreeBSD), see the pure python 'ifconfig' script I posted here:
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
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