forked from StrtCoding/py-tcp-chat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.py
36 lines (27 loc) · 838 Bytes
/
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
import socket
import threading
username = input("Enter your username: ")
host = '127.0.0.1'
port = 55555
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect((host, port))
def receive_messages():
while True:
try:
message = client.recv(1024).decode('utf-8')
if message == "@username":
client.send(username.encode("utf-8"))
else:
print(message)
except:
print("An error Ocurred")
client.close
break
def write_messages():
while True:
message = f"{username}: {input('')}"
client.send(message.encode('utf-8'))
receive_thread = threading.Thread(target=receive_messages)
receive_thread.start()
write_thread = threading.Thread(target=write_messages)
write_thread.start()