/
client.py
37 lines (26 loc) · 905 Bytes
/
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
import asyncio
import os
import aiohttp
HOST = os.getenv('HOST', '0.0.0.0')
PORT = int(os.getenv('PORT', 8080))
URL = f'http://{HOST}:{PORT}/ws'
async def main():
session = aiohttp.ClientSession()
async with session.ws_connect(URL) as ws:
await prompt_and_send(ws)
async for msg in ws:
print('Message received from server:', msg)
await prompt_and_send(ws)
if msg.type in (aiohttp.WSMsgType.CLOSED,
aiohttp.WSMsgType.ERROR):
break
async def prompt_and_send(ws):
new_msg_to_send = input('Type a message to send to the server: ')
if new_msg_to_send == 'exit':
print('Exiting!')
raise SystemExit(0)
await ws.send_str(new_msg_to_send)
if __name__ == '__main__':
print('Type "exit" to quit')
loop = asyncio.get_event_loop()
loop.run_until_complete(main())