-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathserver.py
42 lines (30 loc) · 1009 Bytes
/
server.py
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
import asyncio
import os
import aiohttp.web
HOST = os.getenv('HOST', '0.0.0.0')
PORT = int(os.getenv('PORT', 8080))
async def testhandle(request):
return aiohttp.web.Response(text='Test handle')
async def websocket_handler(request):
print('Websocket connection starting')
ws = aiohttp.web.WebSocketResponse()
await ws.prepare(request)
print('Websocket connection ready')
async for msg in ws:
print(msg)
if msg.type == aiohttp.WSMsgType.TEXT:
print(msg.data)
if msg.data == 'close':
await ws.close()
else:
await ws.send_str(msg.data + '/answer')
print('Websocket connection closed')
return ws
def main():
loop = asyncio.get_event_loop()
app = aiohttp.web.Application(loop=loop)
app.router.add_route('GET', '/', testhandle)
app.router.add_route('GET', '/ws', websocket_handler)
aiohttp.web.run_app(app, host=HOST, port=PORT)
if __name__ == '__main__':
main()