radiacode_docker_usb/testserver.py

40 lines
1.1 KiB
Python
Raw Normal View History

2023-07-05 15:53:17 +03:00
import argparse
import pathlib
from aiohttp import web
file = open(pathlib.Path(__file__).parent.absolute() / 'webserver.html', 'r')
contecnt = file.read()
async def handle_index(request):
return web.Response(content_type='text/html', text=contecnt)
async def handle_ws(request):
ws = web.WebSocketResponse()
await ws.prepare(request)
request.app.ws_clients.append(ws)
async for _ in ws: # noqa: WPS328
pass
request.app.ws_clients.remove(ws)
return ws
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--bluetooth-mac', type=str, required=False, help='bluetooth MAC address of radiascan device')
parser.add_argument('--listen-host', type=str, required=False, default='0.0.0.0', help='listen host for webserver')
parser.add_argument('--listen-port', type=int, required=False, default=8080, help='listen port for webserver')
args = parser.parse_args()
app = web.Application()
app.ws_clients = []
app.add_routes(
[
web.get('/', handle_index)
],
)
web.run_app(app, host=args.listen_host, port=args.listen_port)