Files
oaip-project-app/utils/get_primary_ip.py
2025-05-22 21:57:13 +03:00

24 lines
913 B
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import netifaces
def get_primary_ip():
try:
# Получаем список всех интерфейсов
interfaces = netifaces.interfaces()
for iface in interfaces:
# Пропускаем loopback интерфейс
if iface == 'lo':
continue
# Получаем данные об интерфейсе
iface_data = netifaces.ifaddresses(iface)
# Проверяем наличие IPv4 адреса
if netifaces.AF_INET in iface_data:
for addr in iface_data[netifaces.AF_INET]:
ip = addr['addr']
# Исключаем локальные адреса
if not ip.startswith('127.'):
return ip
return "IP не найден"
except Exception as e:
return f"Ошибка: {e}"