-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.py
More file actions
35 lines (28 loc) · 746 Bytes
/
client.py
File metadata and controls
35 lines (28 loc) · 746 Bytes
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
import socket
import threading
def receive(sock):
while True:
try:
msg = sock.recv(1024).decode()
if not msg:
print("[SERVER CLOSED CONNECTION]")
break
print(msg)
except:
break
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect(("localhost", 12345))
username = input("Enter username: ").strip()
client_socket.send(username.encode())
threading.Thread(
target=receive,
args=(client_socket,),
daemon=True
).start()
while True:
msg = input()
if msg.lower() == "exit":
print("[YOU LEFT THE CHAT]")
client_socket.close()
break
client_socket.send(msg.encode())