13

Possible Duplicate:
Finding local IP addresses using Python's stdlib

To get my localhost IP address I do socket.gethostbyname(socket.gethostname()). But it gives me the answer 127.0.0.1. If I do an_existing_socket.getsockname()[0] I get the answer 0.0.0.0.

I need my 'real' ip address (for instance 192.168.x.x) to modify a configuration file. How could I get it?

Community
  • 1
  • 1
VGO
  • 2,161
  • 2
  • 17
  • 14
  • @BigYellowCactus You're right, I'll look at these answers – VGO Jul 31 '12 at 08:29
  • @Germann Arlington This configuration file is destinated to be used on another host: **1). I update the conf file** with my IP and **2). I launch** remotely an appli that use this conf file. For several reasons, I can't have any control upon the remote host when the appli is launched. – VGO Jul 31 '12 at 08:36
  • @Vaïk Godard - in this case the best solution is to address it by name and let network DNS resolve it to the address. – Germann Arlington Jul 31 '12 at 08:41
  • 1
    The current duplicate link is to a slightly different question that adds "using Python's stdlib." If you can suffer an external package, [How do I determine all of my IP addresses when I have multiple NICs?](http://stackoverflow.com/questions/270745/how-do-i-determine-all-of-my-ip-addresses-when-i-have-multiple-nics) might be more helpful. – jtpereyda Oct 07 '16 at 23:14

2 Answers2

25

I generally use this code:

import os
import socket

if os.name != "nt":
    import fcntl
    import struct

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

def get_lan_ip():
    ip = socket.gethostbyname(socket.gethostname())
    if ip.startswith("127.") and os.name != "nt":
        interfaces = [
            "eth0",
            "eth1",
            "eth2",
            "wlan0",
            "wlan1",
            "wifi0",
            "ath0",
            "ath1",
            "ppp0",
            ]
        for ifname in interfaces:
            try:
                ip = get_interface_ip(ifname)
                break
            except IOError:
                pass
    return ip

I don't know it's origin, but it works on Linux/Windows.

Edit:

This code is used by smerlin in this stackoverflow question.

Community
  • 1
  • 1
sloth
  • 99,095
  • 21
  • 171
  • 219
19

There is a nifty module you can use. Its called netifaces. Just do a pip install netifaces into a virtualenv for testing and try the following code:

import netifaces

interfaces = netifaces.interfaces()
for i in interfaces:
    if i == 'lo':
        continue
    iface = netifaces.ifaddresses(i).get(netifaces.AF_INET)
    if iface != None:
        for j in iface:
            print j['addr']

It all depends on your environment. If you only have one interface with one IP address attached to it, you can simply do:

netifaces.ifaddresses('eth0')[netifaces.AF_INET][0]['addr']

If you are behind a NAT and want to know your public IP address, you can use something like:

import urllib2

ret = urllib2.urlopen('https://icanhazip.com/')
print ret.read()

Hope this helps.

Gabriel Samfira
  • 2,675
  • 1
  • 17
  • 19