-
Notifications
You must be signed in to change notification settings - Fork 0
/
chatcli.py
102 lines (76 loc) · 2.16 KB
/
chatcli.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import socket
import threading
from blessings import Terminal
from queue import Queue
import simple_blessing as SB
IS_EXIT = False
T = Terminal()
H = T.height
L = H // 3
promote = '>>>'
q_user = Queue(L - 1)
q_recv = Queue(L - 1)
q_send = Queue(L - 2)
def online(writer, reader, addr, port, user):
global q_recv
writer.connect((addr, port))
writer.send(user.encode('utf-8'))
data = writer.recv(128)
SB.add2que(q_recv, data.decode("utf-8"))
SB.updateRecv(T, q_recv, L)
if data.decode('utf-8')[0:5] == 'hello':
reader.connect((addr, port))
reader.send(user.encode('utf-8'))
data = reader.recv(128)
if data.decode('utf-8') == 'OK' :
msg = f'{user} is online, type "bye" to offline'
SB.add2que(q_recv, msg)
SB.updateRecv(T, q_recv, L)
def write(writer):
global IS_EXIT
while True:
msg = input('msg>')
SB.add2que(q_send, msg)
SB.freshTer(T, q_user, q_recv, q_send, L)
if IS_EXIT:
break
writer.send(msg.encode('utf-8'))
if msg == 'bye':
IS_EXIT = True
break
def main():
SB.freshTer(T, q_user, q_recv, q_send, L)
global IS_EXIT
writer = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
reader = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
reader.settimeout(1)
online(writer, reader, '127.0.0.1', 9999, 'Mike')
w = threading.Thread(target=write, args=(writer,))
w.start()
while True:
try:
msg = reader.recv(1024)
if not msg:
m = 'server seems closed, type any to exit'
SB.add2que(q_recv, m)
SB.updateRecv(T, q_recv, L)
IS_EXIT = True
break
else:
SB.add2que(q_recv, msg.decode("utf-8"))
SB.updateRecv(T, q_recv, L)
except socket.timeout:
if IS_EXIT:
break
writer.close()
reader.close()
w.join()
print('client is offline')
def add2que(q, e):
if q.full():
q.get()
q.put(e)
else:
q.put(e)
if __name__ == '__main__':
main()