-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathws-client.py
executable file
·57 lines (42 loc) · 1.41 KB
/
ws-client.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/usr/bin/env python3
import argparse
import asyncio
import sys
import websockets
async def consumer(message):
print("\n{}".format(message), end=' ')
async def consumer_handler(websocket):
while True:
try:
message = await websocket.recv()
await consumer(message)
except:
break
async def producer(loop):
return await loop.run_in_executor(None, sys.stdin.readline)
async def producer_handler(websocket):
while True:
message = await producer(asyncio.get_event_loop())
await websocket.send(message.rstrip())
async def client(ip):
async with websockets.connect('ws://{ip}:4000'.format(ip=ip)) as websocket:
consumer_task = asyncio.ensure_future(consumer_handler(websocket))
producer_task = asyncio.ensure_future(producer_handler(websocket))
done, pending = await asyncio.wait(
[consumer_task, producer_task],
return_when=asyncio.FIRST_COMPLETED,
)
for task in pending:
task.cancel()
def main(args):
parser = argparse.ArgumentParser(description='npmud client')
parser.add_argument(
'--server',
default='localhost',
help='The remote npmud server to connect to',
dest='server',
)
args = parser.parse_args()
asyncio.get_event_loop().run_until_complete(client(args.server))
if __name__ == '__main__':
main(sys.argv[1:])