How can I get the IP address of eth0 in Python?

When an error occurs in a Python script on Unix , an email is sent.

I have been asked to add {Testing Environment} to the subject line of the email if the IP address is 192.168.100.37 which is the testing server. This way we can have one version of a script and a way to tell if the email is coming from messed up data on the testing server.

However, when I google I keep finding this code:

    import socket
    socket.gethostbyname(socket.gethostname())

However, that's giving me the IP address of 127.0.1.1. When I use ifconfig I get this

    eth0      Link encap:Ethernet  HWaddr 00:1c:c4:2c:c8:3e
              inet addr:192.168.100.37  Bcast:192.168.100.255  Mask:255.255.255.0
              UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
              RX packets:75760697 errors:0 dropped:411180 overruns:0 frame:0
              TX packets:23166399 errors:0 dropped:0 overruns:0 carrier:0
              collisions:0 txqueuelen:1000
              RX bytes:59525958247 (59.5 GB)  TX bytes:10142130096 (10.1 GB)
              Interrupt:19 Memory:f0500000-f0520000

    lo        Link encap:Local Loopback
              inet addr:127.0.0.1  Mask:255.0.0.0
              UP LOOPBACK RUNNING  MTU:16436  Metric:1
              RX packets:25573544 errors:0 dropped:0 overruns:0 frame:0
              TX packets:25573544 errors:0 dropped:0 overruns:0 carrier:0
              collisions:0 txqueuelen:0
              RX bytes:44531490070 (44.5 GB)  TX bytes:44531490070 (44.5 GB)

Firstly, I don't know where it got 127.0.1.1 from, but either way that's not what I want. When I google I keep coming to the same syntax, Bash scripts or netifaces and I'm trying to use standard libraries.

So how can I get the IP address of eth0 in Python?

Two methods:

Method #1 (use external package)

You need to ask for the IP address that is bound to your eth0 interface. This is available from the netifaces package

    import netifaces as ni
    ni.ifaddresses('eth0')
    ip = ni.ifaddresses('eth0')[ni.AF_INET][0]['addr']
    print ip  # should print "192.168.100.37"

You can also get a list of all available interfaces via

    ni.interfaces()

Method #2 (no external package)

Here's a way to get the IP address without using a python package:

    import socket
    import fcntl
    import struct

    def get_ip_address(ifname):
        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        return socket.inet_ntoa(fcntl.ioctl(
            s.fileno(),
            0x8915,  # SIOCGIFADDR
            struct.pack('256s', ifname[:15])
        )[20:24])

    get_ip_address('eth0')  # '192.168.0.110'

Note: detecting the IP address to determine what environment you are using is quite a hack. Almost all frameworks provide a very simple way to set/modify an environment variable to indicate the current environment. Try and take a look at your documentation for this. It should be as simple as doing

    if app.config['ENV'] == 'production':
      #send production email
    else:
      #send development email

From: stackoverflow.com/q/24196932

Back to homepage or read more recommendations: