-
-
Notifications
You must be signed in to change notification settings - Fork 141
Expand file tree
/
Copy pathweb.py
More file actions
executable file
·30 lines (26 loc) · 946 Bytes
/
web.py
File metadata and controls
executable file
·30 lines (26 loc) · 946 Bytes
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
from aiohttp import web
import pathlib
import argparse
async def websocket_handler(request):
ws = web.WebSocketResponse()
await ws.prepare(request)
async for msg in ws:
if msg.type == web.WSMsgType.TEXT:
await ws.send_str("Echo: " + msg.data)
elif msg.type == web.WSMsgType.CLOSED:
break
return ws
app = web.Application()
app.router.add_get("/ws", websocket_handler)
# Serve static files
current_path = pathlib.Path(__file__).parent
static_path = current_path / "static"
print(static_path)
app.router.add_static("/", path=static_path, name="static")
if __name__ == '__main__':
# Parse command-line arguments
parser = argparse.ArgumentParser(description='Run WebSocket server')
parser.add_argument('--port', type=int, default=8080,
help='Port to run the server on (default: 8080)')
args = parser.parse_args()
web.run_app(app, port=args.port)