This repository has been archived by the owner on May 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
joiner.py
59 lines (49 loc) · 1.69 KB
/
joiner.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
import asyncio
import sys
from ip import port
from concurrent.futures import ThreadPoolExecutor
ip = 'enter.server.port.here'
async def ainput() -> str:
with ThreadPoolExecutor(1, "AsyncInput") as executor:
return await asyncio.get_event_loop().run_in_executor(executor, input)
async def send_data(writer, name):
while True:
try:
message = await ainput()
message = name + "|" + message
data = message.encode()
data += b' ' * (1024 - len(data))
writer.write(data)
await writer.drain()
await asyncio.sleep(1/12)
# print("drained properly")
except KeyboardInterrupt as err:
writer.write("!disconnect".encode())
await writer.drain()
print("chat closed")
return
async def receive_data(reader):
count = 0
while True:
# print(f"receive_data {count}")
try:
data = await reader.read(1024)
name, message = data.decode().split('|')
print(f"{name}:\t", message.rstrip())
except KeyboardInterrupt as err:
return
count += 1
async def main(name):
print(f'joining chat at {ip}:{port}')
reader, writer = await asyncio.open_connection(ip, port)
send_task = asyncio.create_task(send_data(writer, name))
receive_task = asyncio.create_task(receive_data(reader))
await asyncio.gather(send_task, receive_task)
# try:
# await asyncio.wait([send_task, receive_task], timeout=3600)
# finally:
# send_task.cancel()
if __name__ == "__main__":
# name = input('Enter your name:\t')
name = "jesus"
asyncio.run(main(name))