add host ip detection

master
gtitov 3 years ago
parent f57f680b78
commit cf355234c8

@ -28,6 +28,7 @@
<body>
<header id="header">
<h1>Тестирование</h1>
<p id="host-ip">Тест работает по адресу</p>
<p>Получите тест</p>
</header>

@ -4,6 +4,10 @@ document.addEventListener("DOMContentLoaded", function () {
var students_selector = document.getElementById("students-selector")
var get_quiz_button = document.getElementById("get-quiz")
fetch("/hostip")
.then(r => r.json())
.then(host_ip => document.getElementById("host-ip").innerText += ` ${host_ip}:8000`)
fetch("/students")
.then(r => r.json())
.then(students => {

@ -4,7 +4,8 @@ from fastapi.staticfiles import StaticFiles
import json
import random
from pathlib import Path
from datetime import datetime
from datetime import datetime
import socket
from settings import WAVE, END_TEST_PASSWORD, QUIZ_LENGTH, QUESTIONS_FILE, STUDENTS_FILE
@ -61,6 +62,20 @@ def check_answers(student_answers: dict):
return checked_answers
@app.get("/hostip")
def show_host_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
@app.get("/students")
def show_students():
return students

Loading…
Cancel
Save