-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.py
More file actions
44 lines (30 loc) · 1017 Bytes
/
Client.py
File metadata and controls
44 lines (30 loc) · 1017 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
36
37
38
39
40
41
42
43
44
import socket
import threading
nickname = input("Choose your nickname before joining server: ")
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# this is the ip of the server that we want to connect to
client.connect(('127.0.0.1', 55555))
def receive():
while True:
try:
# receiving from the server
message = client.recv(1024).decode('ascii')
if message == 'NICK':
client.send(nickname.encode('ascii'))
else:
print(message)
except:
(print("An error Occurred!"))
client.close()
break
def write():
while True:
message = f'{nickname}: {input("")}'
client.send(message.encode('ascii'))
# we are running 2 threads receive thread and to write thread
# the thread for receiving
receive_thread = threading.Thread(target=receive)
receive_thread.start()
# the thread for writing
write_thread = threading.Thread(target=write)
write_thread.start()