-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.py
executable file
·55 lines (48 loc) · 1.79 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
48
49
50
51
52
53
54
55
import socket
import errno
import sys
import threading
HEADER_LENGTH = 4096
IP = "127.0.0.1"
PORT = 9876
class ChatClient(object):
def __init__(self):
username = input('Enter Username: ')
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect((IP, PORT))
client_socket.settimeout(1000)
client_socket.setblocking(False)
self.client_socket = client_socket
self.username: str = username
# :< here is formatting basically, we are trying to keep number left aligned here
# username_header = f"{len(username):<{HEADER_LENGTH}}"
# print(username_header)
client_socket.send(self.username.encode("utf-8"))
threading.Thread(target=self.message_receiver).start()
threading.Thread(target=self.message_sender).start()
def message_sender(self):
while True:
message = input(f"#{str(self.username)} >>")
if message:
message = message.encode('utf-8')
self.client_socket.send(message)
def message_receiver(self):
while True:
try:
# Receive Message
# First get bytes of username
data = self.client_socket.recv(HEADER_LENGTH).decode("utf-8")
if not data:
print("Connection closed by server")
sys.exit()
print(data)
except IOError as e:
if e.errno != errno.EAGAIN and e.errno != errno.EWOULDBLOCK:
print('[Error] While Reading', str(e))
sys.exit()
continue
except Exception as e:
print('[Error]', str(e))
sys.exit()
if __name__ == '__main__':
client = ChatClient()