Clean way to get one's network IP address?

Roy Smith roy at
Wed Nov 21 17:39:57 CET 2007

Gilles Ganault <nospam at> wrote:

> 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?

More information about the Python-list mailing list