def get_location_by_ip(ip):
if ip == '127.0.0.1':
return "Локальный хост"
try:
# Важно: используй http, так как бесплатный тариф может не поддерживать https
response = requests.get(f'http://ip-api.com/json/{ip}?lang=ru', timeout=5)
data = response.json()
if data.get('status') == 'success':
return f"{data.get('city')}, {data.get('country')}"
return f"Ошибка API: {data.get('message')}"
except Exception as e:
return f"Ошибка запроса: {str(e)}"