navpoint/navpoint/get_ip.py
2024-07-21 13:27:27 -07:00

23 lines
551 B
Python
Executable File

# Copyright 2015-2021 Stack Overflow user "fatal_error"
# Copyright 2020 Stack Overflow user "user2561747"
#
# https://stackoverflow.com/a/28950776
#
# Used under CC BY-SA 4.0: https://creativecommons.org/licenses/by-sa/4.0/
import socket
def get_ip():
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.settimeout(0)
try:
# doesn't even have to be reachable
s.connect(("10.254.254.254", 1))
IP = s.getsockname()[0]
except Exception:
IP = "127.0.0.1"
finally:
s.close()
return IP