[Tutor] Alternatives to get IP address of a computer : which one should I use ?

Eike Welk eike.welk at gmx.net
Tue Nov 24 23:44:27 CET 2009


On Tuesday 24 November 2009, Shashwat Anand wrote:
On my openSuse 11.0 machine your method doesn't work as intended:


eike at lixie:~> python
Python 2.5.2 (r252:60911, Dec  1 2008, 18:10:01)
[GCC 4.3.1 20080507 (prerelease) [gcc-4_3-branch revision 135036]] on 
linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import socket
>>> socket.gethostbyname(socket.gethostname())
'127.0.0.2'
>>>


It's a valid IP of my computer, but not the one you wanted. You could 
have written this one from memory (well I would have written 
127.0.0.1, which is also valid).  

Parsing the output from ifconfig would work for my computer. 


Kind regards,
Eike.


More information about the Tutor mailing list