Clean way to get one's network IP address?

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Fri Nov 23 23:56:41 CET 2007


On Fri, 23 Nov 2007 08:52:58 -0800, drobinow at gmail.com wrote:

> On Nov 21, 5:34 pm, Steven D'Aprano <st... at 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... at 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.



More information about the Python-list mailing list