Machine identification

Jeff Epler jepler at
Wed Sep 8 22:53:02 CEST 2004

I don't have a machine with the particular misconfiguration John does,
but I have suggested a different method which should identify the IP
address of the external interface, not the loopback device.

    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    s.connect(("", 1))
    print s.getsockname()

No packets are actually sent on s, so it doesn't matter that
probably doesn't exist, or in any case doesn't intend to receive UDP
packets on port 1.  It's just a routable IPV4 address.

Once the socket is created, its "name" will be filled out, and should
correspond to the address of the interface that would actually be used
to send the packet.

If this address is a non-routable address, such as, which will
be NATted by some other machine, the problem can't be solved by writing
a few lines of Python to execute on the NATted machine.  However, this
routine will help when DNS is misconfigured (so that gethostbyname
returns the wrong address) or when gethostname misbehaves (returning the
wrong string, like "localhost.localdomain")

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 196 bytes
Desc: not available
URL: <>

More information about the Python-list mailing list