Simulateur
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
#!/usr/bin/python3.7
|
||||
|
||||
import aiohttp
|
||||
from aiohttp import web, WSCloseCode
|
||||
import asyncio
|
||||
|
||||
|
||||
async def http_handler(request):
|
||||
return web.Response(text='Hello, world')
|
||||
|
||||
|
||||
async def websocket_handler(request):
|
||||
ws = web.WebSocketResponse()
|
||||
await ws.prepare(request)
|
||||
|
||||
async for msg in ws:
|
||||
if msg.type == aiohttp.WSMsgType.TEXT:
|
||||
if msg.data == 'close':
|
||||
await ws.close()
|
||||
else:
|
||||
await ws.send_str('some websocket message payload')
|
||||
elif msg.type == aiohttp.WSMsgType.ERROR:
|
||||
print('ws connection closed with exception %s' % ws.exception())
|
||||
|
||||
return ws
|
||||
|
||||
|
||||
def create_runner():
|
||||
app = web.Application()
|
||||
app.add_routes([
|
||||
web.get('/', http_handler),
|
||||
web.get('/ws', websocket_handler),
|
||||
])
|
||||
return web.AppRunner(app)
|
||||
|
||||
|
||||
async def start_server(host="127.0.0.1", port=1337):
|
||||
runner = create_runner()
|
||||
await runner.setup()
|
||||
site = web.TCPSite(runner, host, port)
|
||||
await site.start()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
loop = asyncio.get_event_loop()
|
||||
loop.run_until_complete(start_server())
|
||||
loop.run_forever()
|
||||
Reference in New Issue
Block a user