Getting local IP address...

Mike Fletcher mfletch at tpresence.com
Tue Jul 18 19:38:15 EDT 2000


The primary problem with #1 is complex network configurations. For instance,
here is what the code gives you on my machine (and my configuration is not
that complex in the grand scheme of things)...

>>> import socket
>>> socket.gethostbyname( socket.gethostname())
'192.0.2.201'

Which seems perfectly reasonable, except that that is actually an address on
our VPN, and is completely un-addressable by the outside world.
(Incidentally, you likely get the same information from the Linux hackery,
since it only accesses "the first" ethernet adapter, is, effectively, doing
exactly the same thing as gethostbyname, just in an obscure way, and you
still are left not knowing what is connected to the Internet).

>>> socket.gethostbyname_ex( socket.gethostname())
('cr706570-a.yec1.on.wave.home.com', [], ['192.0.2.201', '24.112.128.169',
'172.16.0.1'])

Shows the three different addresses for my machine.  Only the second address
is a public IP address.  If I were to publish either of the other two
addresses, no one would be able to connect to me using them (unless they
happen to be either on our VPN or using my testing computer on its little
testing network (which, points up the neat feature of the testing approach,
as I can actually use the same code for all three networks, simply changing
the "remote address" I use to establish my own IP address)).

Of course, as Donn mentioned, if you already have a connection, it is really
cheap to simply read off the address from that connection.  One of our
(older) projects even uses reflection, having the server report to the
client the public address from which the client appears to be connecting
(which, incidentally, lets you notice that you are behind a firewall or the
like).  That, of course, requires a server somewhere :o) .

So, for 80% of the end-users out there, who only have a single IP address,
no big deal, you can use the DNS lookups (gethostbyname, gethostname).  If
this is just for "information" purposes, you could use the DNS lookups
without a problem.  For anyone with a more complex networking structure, you
may run into problems.

The choice is yours.
Networking is an ugly mess.
Use the address wisely, Luke.
Multi-homed systems are the sign of the devil, don't trust anyone using one.
All who would defy nature and put more than one NIC in a computer are lying
scoundrels!

Mike


-----Original Message-----
From: stephen at cerebralmaelstrom.com.bbs@openbazaar.net
[mailto:stephen at cerebralmaelstrom.com.bbs@openbazaar.net]
Sent: Tuesday, July 18, 2000 4:40 AM
To: python-list at python.org
Subject: Re: Getting local IP address...


    Um, jeez. I'm rather surprised by these responses -- I assumed it
was some simple little function somewhere I was missing.. I'm also
even more surprised at the differences between the three approaches

-- #1 -- socket.gethostbyname(socket.gethostname())
-- #2 -- Some complicated stuff dealing with asking Linux (which,
incidentely, is bad since i'm primarily on a Windows machine for
the time being -- and in the future will be on a *BSD, and hope
to have my program working on all three :)) what the address is
on the given ethernet card..
-- #3 -- Just go all out and connect, since in the process you
have to be telilng the other side where you are, so you can
get it that way.

#2 and #3 just seem .. convoluted .. as heck. But because so
many people sugguested them, I have to assume that they
beileve there is some central problem with #1... What is it? :)

...




More information about the Python-list mailing list