Determine ip address

Lee Harr lee at example.com
Fri Apr 15 16:46:54 EDT 2005


On 2005-04-15, codecraig <codecraig at gmail.com> wrote:
> hi,
>    how can i use python to figure the ip address of the machine which
> the python script is running on?  I dont mean like 127.0.0.1....but i
> want the external IP address (such as ipconfig on windows displays).
>
> any ideas??
>



I use this:

#conf.py
ifconfig = '/sbin/ifconfig'
iface = 'eth0'
telltale = 'inet addr:'


#addr.py
import commands

from conf import ifconfig, iface, telltale

def my_addr():
    cmd = '%s %s' % (ifconfig, iface)
    output = commands.getoutput(cmd)

    inet = output.find(telltale)
    if inet >= 0:
        start = inet + len(telltale)
        end = output.find(' ', start)
        addr = output[start:end]
    else:
        addr = ''




More information about the Python-list mailing list