diff --git a/demo/func/__init__.py b/demo/func/__init__.py new file mode 100644 index 0000000..10c0679 --- /dev/null +++ b/demo/func/__init__.py @@ -0,0 +1 @@ +from . import secondary_functions \ No newline at end of file diff --git a/demo/func/secondary_functions.py b/demo/func/secondary_functions.py new file mode 100644 index 0000000..9525d93 --- /dev/null +++ b/demo/func/secondary_functions.py @@ -0,0 +1,21 @@ +import time +import asyncio +from aiohttp.web_ws import WebSocketResponse + + +async def time_response(ws: WebSocketResponse, delay: int = 60): + while True: + new_time: time.gmtime = time.gmtime(time.time()) + hour = new_time.tm_hour + 3 + if hour >= 24: + hour = hour - 24 + minutes = new_time.tm_min + if minutes < 10: + minutes = f'0{minutes}' + result_response = f'Время: {hour}:{minutes}' + try: + await ws.send_str(result_response) + await asyncio.sleep(delay) + except ConnectionResetError: + break + \ No newline at end of file diff --git a/demo/routes.py b/demo/routes.py index 3940b96..4b3996e 100644 --- a/demo/routes.py +++ b/demo/routes.py @@ -3,5 +3,9 @@ import aiohttp def setup_routes(app): - app.router.add_routes([aiohttp.web.get('/', frontend.index), aiohttp.web.post('/submit', frontend.button_clicked)]) + app.router.add_routes([aiohttp.web.get('/index.html', frontend.index), + aiohttp.web.post('/submit', frontend.button_clicked), + aiohttp.web.get('/index_2.html', frontend.index_2), + aiohttp.web.get('/ws', frontend.websocket_handler),] + ) \ No newline at end of file diff --git a/demo/templates/index.html b/demo/templates/index.html index b0613b1..3bdd8bd 100644 --- a/demo/templates/index.html +++ b/demo/templates/index.html @@ -10,31 +10,35 @@ body { min-height: 100vh; - background: linear-gradient(90deg, rgba(2,0,36,1) 0%, rgba(9,9,121,1) 35%, rgba(0,212,255,1) 100%) + background: linear-gradient( + 90deg, + rgba(2, 0, 36, 1) 0%, + rgba(9, 9, 121, 1) 35%, + rgba(0, 212, 255, 1) 100% + ); } - h1{ + h1 { color: white; - } - p{ - color:white; + p { + color: white; } - .container{ + .container { margin-left: 20vw; } - #submit{ + #submit { margin-top: 20px; border-radius: 10px; height: 50px; - width:50vw; - color:black; + width: 50vw; + color: black; } - #inputText{ + #inputText { border-radius: 10px; height: 30px; width: 49.5vw; } - .container_result{ + .container_result { margin-top: 20px; min-height: 50px; text-align: center; @@ -43,8 +47,6 @@ border-radius: 5px; background-color: grey; } - - @@ -53,35 +55,41 @@
- -
- + +
+ +

+
+ Перейти на страницу 2 +
-
+ - + + const responseText = await response.text(); + document.getElementById("response").innerText = responseText; + }); + diff --git a/demo/templates/index_2.html b/demo/templates/index_2.html new file mode 100644 index 0000000..c3b5980 --- /dev/null +++ b/demo/templates/index_2.html @@ -0,0 +1,92 @@ + + + + + Знакомство с aiohttp + + + +
+

Дата и время

+
+
+
+

+
+
+
+ + +
+
+
+

+
+
+ + + diff --git a/demo/views/__init__.py b/demo/views/__init__.py index 20f280e..910cb7a 100644 --- a/demo/views/__init__.py +++ b/demo/views/__init__.py @@ -1 +1,2 @@ from . import frontend + diff --git a/demo/views/frontend.py b/demo/views/frontend.py index b5f5ecf..7919dc0 100644 --- a/demo/views/frontend.py +++ b/demo/views/frontend.py @@ -1,16 +1,38 @@ +import asyncio import aiohttp - +from aiohttp import WSMsgType +from aiohttp.web_response import Response +from aiohttp.web_ws import WebSocketResponse from aiohttp_jinja2 import template - - +from ..func import secondary_functions + + @template('index.html') async def index(request): return {} +@template('index_2.html') +async def index_2(request): + return {} + + +async def websocket_handler(request): + ws: WebSocketResponse = WebSocketResponse() + await ws.prepare(request) + asyncio.create_task(secondary_functions.time_response(ws, delay=60)) + async for msg in ws: + if msg.type == aiohttp.WSMsgType.TEXT: + await ws.send_str(f'Вы ввели: {msg.data}') + elif msg.type == WSMsgType.ERROR: + print('ws connection closed with exception %s' % + ws.exception()) + print('websocket connection closed') + return ws + async def button_clicked(request): data = await request.post() text = data.get('text') response_text = f'Вы ввели: {text}' print(response_text) - return aiohttp.web.Response(text=response_text) \ No newline at end of file + return Response(text=response_text) \ No newline at end of file diff --git a/entry.py b/entry.py index cb4d3e3..73db430 100644 --- a/entry.py +++ b/entry.py @@ -6,3 +6,4 @@ app = create_app() if __name__ == '__main__': aiohttp.web.run_app(app) +