-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver1.py
More file actions
executable file
·42 lines (32 loc) · 1.75 KB
/
Copy pathserver1.py
File metadata and controls
executable file
·42 lines (32 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!/usr/bin/env python3.7
################################################################################
# Первая версия сервера.
# Для ознакомления с взаимодействием браузера, JavaScript в браузере,
# WebSocket-ов, сервера на Python.
################################################################################
import logging
import quart
app = quart.Quart(__name__, template_folder="templates", static_folder="static")
log = logging.getLogger(__name__)
logging.basicConfig(level=logging.DEBUG, format="> %(asctime)-15s %(levelname)-8s || %(message)s")
@app.route("/")
async def index():
return await quart.render_template("index.html")
@app.route("/p/<page>")
async def handle_p(page):
return await quart.render_template(f"p_{page}.html")
@app.websocket("/ws")
async def handle_ws():
# Функция вызывается при установлении WebSocket-соединения между браузером и сервером.
# Каждый раз, когда вы открываете новое окно браузера, происходит новый вызов данной функции.
# Функция выполяется до тех пор, пока живо соединение между браузером и сервером.
while True:
data = await quart.websocket.receive()
log.info("Получили от браузера %r", data)
await quart.websocket.send(
f"Подтверждаем, что получили '{data}'! Текст сгенерирован на сервере.")
def main():
app.config.from_mapping(DEBUG=True, ENV="development")
app.run()
if __name__ == "__main__":
main()