-
Notifications
You must be signed in to change notification settings - Fork 0
/
client2.py
46 lines (38 loc) · 1.05 KB
/
client2.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
import socket
import threading
stop = False
def listen(s: socket.socket) -> None:
global stop
while True:
try:
message = s.recv(1024).decode()
if message in ('$break', 'shutdown'):
print("Disconnected.")
stop = True
break
elif not message:
stop = True
break
print('\r\r' + message + '\n' + 'you: ', end='')
except ConnectionResetError:
print("Server off.")
stop = True
break
def connect(host: str = '127.0.0.1', port: int = 1025) -> None:
global stop
s = socket.socket()
s.connect((host, port))
threading.Thread(target=listen, args=(s,), daemon=True).start()
while True:
message = input('you: ')
if stop:
break
else:
try:
s.send(message.encode())
except ConnectionResetError:
pass
if message in ('exit', 'shutdown'):
break
s.close()
connect()