how to obtain its ip address ?

Daniel Yoo dyoo at hkn.eecs.berkeley.edu
Wed Jun 23 22:42:26 CEST 2004


marco <manatlan at online.fr> wrote:
: I'd search a lot ... but i had not found
: how can i obtain my ip address (when i'm connected to the www) ?!


Hi Marco,


Here's a quicky way to do it:

###
import socket

def getSelfIp():
    """Returns the IP address of the local host."""
    return socket.gethostbyname(socket.gethostname())
###


This might not work perfectly.  If you want something more
comprehensive, you may want to use something like the localIP()
function in Webware:

    http://webware.sourceforge.net/Webware-0.8b2/MiscUtils/Docs/Source/Files/Funcs.py.html

This appears to be a bit more systematic in getting a good local IP
address.


Good luck to you!



More information about the Python-list mailing list