summaryrefslogtreecommitdiff
path: root/resources/external/websocket_test.py
blob: 9c29fcf76ee15293796bad69e4026a72e3572a3e (plain)
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
#!/usr/bin/env python

"""Echo server using the asyncio API."""

import asyncio
from websockets.asyncio.server import serve

DEFAULT_PORT = 8181
DEFAULT_IP = "127.0.0.1"


async def echo(websocket):
    print("client connected")
    async for message in websocket:
        await websocket.send(message)


async def main(ip, port):
    async with serve(echo, ip, port) as server:
        print("serving...")
        await server.serve_forever()


if __name__ == "__main__":
    IP = DEFAULT_IP
    PORT = DEFAULT_PORT
    args = sys.argv[1:]
    for arg_i in range(len(args)):
        if args[arg_i] == "-ip" and arg_i < (len(args)-1):
            arg_i += 1
            IP = args[arg_i]
        if args[arg_i] == "-port" and arg_i < (len(args)-1):
            arg_i += 1
            try:
                PORT = int(args[arg_i])
            except:
                print("Port must be an integer.")
                return

    asyncio.run(main(IP, PORT))