-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.py
38 lines (31 loc) · 861 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
37
38
import socket
from threading import Thread
nickname = input("Enter a nickname: ")
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect(("127.0.0.1", 6969))
def recieve():
"""
Recieving messages from server.
"""
while True:
try:
message = client.recv(1024).decode("ascii")
if message == "NICK":
client.send(nickname.encode("ascii"))
else:
print(message)
except Exception as e:
print(e)
print("Some error occured!")
client.close()
break
def send():
while True:
message = f"{nickname}: {input()}"
client.send(message.encode("ascii"))
# recieving thread
r_thread = Thread(target=recieve)
r_thread.start()
# sending thread
s_thread = Thread(target=send)
s_thread.start()