-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.py
47 lines (40 loc) · 1.66 KB
/
client.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
import socket, threading
def main():
serverName = '127.0.0.1'
serverPort = 12000
clientSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
clientSocket.connect((serverName, serverPort))
print("\n************************************************************")
print("*** Simple Chat APP 1.0.0 ***")
print(f"*** {clientSocket.recv(1024).decode('utf-8')} ***")
print("*** Send your message in format '#clientID:message' ***")
print("*** Send '/getlist' to update available client list ***")
print("*** Send '/getblocklist' to see blocklist ***")
print("*** Send '/block clientID' to block a client ***")
print("*** Send '/unblock clientID' to unblock a client ***")
print("*** Send '.exit' to disconnect from server ***")
print("************************************************************")
print(f"{clientSocket.recv(1024).decode('utf-8')}")
def send():
"""send message"""
while 1:
send_msg = input("")
clientSocket.send(send_msg.encode('utf-8'))
if send_msg == '.exit':
break
def receive():
"""receive message"""
while 1:
recv_msg = clientSocket.recv(1024).decode('utf-8')
if recv_msg == '.exit' or len(recv_msg) == 0:
print('Disconnected from Server')
break
print(recv_msg)
s = threading.Thread(target=send)
r = threading.Thread(target=receive)
s.start()
r.start()
s.join()
r.join()
if __name__ == "__main__":
main()