5

There's a code I found in internet that says it gives my machines local network IP address:

hostname = socket.gethostname()
local_ip = socket.gethostbyname(hostname)

but the IP it returns is 192.168.94.2 but my IP address in WIFI network is actually 192.168.1.107 How can I only get wifi network local IP address with only python? I want it to work for windows,linux and macos.

2

4 Answers 4

10

You can use this code:

import socket
hostname = socket.getfqdn()
print("IP Address:",socket.gethostbyname_ex(hostname)[2][1])

or this to get public ip:

import requests
import json
print(json.loads(requests.get("https://ip.seeip.org/jsonip?").text)["ip"])
Sign up to request clarification or add additional context in comments.

3 Comments

Modified last index 1 to 0, to work on my setup: socket.gethostbyname_ex(hostname)[2][0]
On Raspberry Pi, it only gets the trivial IP 127.0.1.1
It doesn't work on MacOS either. says 127.0.0.1
1

Modified as print("IP Address:",socket.gethostbyname(hostname)) work for me. Using Windows 10.

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
1

To my research we HAVE TO use OS shell commands to get it right.

import os, subprocess, platform, ipaddress

def run_shell_command(command, capture_output = False):
    print(f"$ {command}")
    return subprocess.run(command, shell=True, capture_output=capture_output, text=capture_output) # 'shell=True' => Use built-in shell, '/bin/sh' on Linux and Mac, 'cmd.exe' on Windows.


def get_local_ipv4():
    local_ipv4 = None

    operating_system_kind = platform.system()
    if operating_system_kind == 'Darwin':
        local_ipv4 = run_shell_command('ipconfig getifaddr en0', True).stdout.strip()
    elif operating_system_kind == 'Windows':
        for line in run_shell_command('netsh ip show address | findstr "IP Address"', True).stdout.split(os.linesep):
            local_ipv4_or_empty = line.removeprefix('IP Address:').strip()
            if local_ipv4_or_empty:
                local_ipv4 = local_ipv4_or_empty
                break
    else: # Linux
        local_ipv4 = run_shell_command("hostname -I | awk '{print $1}'", True).stdout.strip()

    try:
        ipaddress.IPv4Address(local_ipv4)
    except ipaddress.AddressValueError:
        raise Exception("Could not get local network IPv4 address for Expo Fronted")

    if local_ipv4.startswith("127.0."):
        raise Exception(f"Could not get correct local network IPv4 address for Expo Fronted, got {local_ipv4}")

    return local_ipv4

local_ipv4 = get_local_ipv4()

Comments

0

On my Raspberry (bookworm) the socket.getfqdn() returns only the pure host name, not the fully qualified name! And with that the socket.gethostbyname_ex returns only 127.0.0.1. According to socket.getfqdn() and socket.gethostname() are giving Different IP addresses when using socket.gethostname the socket.getfqdn() is reading the file /etc/hosts.

The shell command hostname -A returns two times the FQDN on my Raspberry. The shell command hostname -I returns all IP addresses, starting with the IPv4 (if any!?), followed by IPv6 addresses:

  y = subprocess.run(['/usr/bin/hostname', '-I'], capture_output=True)
  ipAddrs = y.stdout.split()
  ipv4 = ipAddrs[0]

I am not sure, whether the last address ipAddrs[-1] is always a link local address fd00:... or that's only so in my case.

On other systems the hostname -I may be not available, e.g. Synology NAS DSM 7.2.

1 Comment

You say "On other systems blah blah"; it might be worth clarifying that as of the present, the busybox (used widely in embedded linux systems) implementation of hostname doesn't support that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.