-
Notifications
You must be signed in to change notification settings - Fork 9
/
clients.py
43 lines (30 loc) · 1.16 KB
/
clients.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
import asyncio
import datetime
HOST = '127.0.0.1'
PORT = 55555
BUFSIZE = 4096
def print_indent(indent, string):
t = datetime.datetime.fromtimestamp(asyncio.get_event_loop().time())
print('\t' * indent + f'[{t:%S.%f}] ' + string)
async def client(name, indent):
print_indent(indent, f'Client {name} tries to connect.')
reader, writer = await asyncio.open_connection(host=HOST, port=PORT)
# first make dummy write and read to show that the server talks to us
writer.write(b'*')
await writer.drain()
resp = await reader.read(BUFSIZE)
print_indent(indent, f'Client {name} connects.')
for msg in ['Hello', 'world!',]:
await asyncio.sleep(0.5)
writer.write(msg.encode())
await writer.drain()
print_indent(indent, f'Client {name} sends "{msg}".')
resp = (await reader.read(BUFSIZE)).decode()
print_indent(indent, f'Client {name} receives "{resp}".')
writer.close()
print_indent(indent, f'Client {name} disconnects.')
async def main():
clients = [asyncio.create_task(client(i, i)) for i in range(3)]
await asyncio.wait(clients)
if __name__ == '__main__':
asyncio.run(main())