Getting local IP address...

David Arnold arnold at dstc.monash.edu.au
Tue Jul 18 21:05:12 EDT 2000


-->"Stephen" == Stephen Hansen <stephen%cerebralmaelstrom.com.bbs at openbazaar.net> writes:

  Stephen> Um, jeez. I'm rather surprised by these responses -- I
  Stephen> assumed it was some simple little function somewhere I was
  Stephen> missing.

sadly not.  in the simple case, gethostbyname(gethostname()) will do
what you want, but for machines with multiple interfaces it's not
guaranteed to get you a useful IP address.

this is particularly the case for a machine with a dialup PPP link,
and a local ethernet card using (just to exhibit some more complexity)
a NATed private address range.  in this case, it's likely that the
reverse lookup on the hostname would return the NATed address, which
is useless to external hosts.

the third approach has load implications, but is at least portable and
reliable ...


i've been looking at the available facilities (under Unix) for finding
interfaces and addresses, and it's reasonably arcane, and has a lot of
portability warning, but it's tempting to write a small module to
expose the functionality.

one complication i found today is that h2py (needed to get the
constants for use with ioctl()) doesn't support multi-parameter
macros, and consequently IOCTL on Solaris doesn't work ...

anyone have plans to extend h2py for 1.6/2.0 to support multi-arg
macro translations?

anyone interested in a general interface info module?



d




More information about the Python-list mailing list