-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.py
60 lines (41 loc) · 1.33 KB
/
utils.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
58
59
60
import asyncio
def custom_handler(loop, context):
exception = context.get("exception")
if isinstance(exception, KeyboardInterrupt):
# Ignore it
return
else:
# Call the default exception handler
loop.default_exception_handler(context)
async def read_msg(reader):
try:
msg_len = int.from_bytes(await reader.readexactly(2), byteorder="big")
return await reader.readexactly(msg_len)
except asyncio.IncompleteReadError:
return None
async def write_msg(writer, msg):
msg_len = len(msg)
msg_len = msg_len.to_bytes(2, byteorder="big")
if isinstance(msg, str):
msg = msg.encode()
writer.write(msg_len + msg)
await writer.drain()
async def write_msg_slow_network(writer, msg, chunk_size=2):
msg_len = len(msg)
msg_len = msg_len.to_bytes(2, byteorder="big")
if isinstance(msg, str):
msg = msg.encode()
writer.write(msg_len)
await writer.drain()
for i in range(0, len(msg), 2):
await asyncio.sleep(0.1)
writer.write(msg[i:i+chunk_size])
await writer.drain()
class TermColors:
PURPLE = '\033[95m'
BLUE = '\033[94m'
CYAN = '\033[96m'
GREEN = '\033[92m'
YELLOW = '\033[93m'
RED = '\033[91m'
ENDC = '\033[0m'