-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
130 lines (113 loc) · 4.71 KB
/
server.py
File metadata and controls
130 lines (113 loc) · 4.71 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import socket
import threading
format = 'utf-8'
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host_port = ('127.0.0.1', 8000)
s.bind(host_port)
print('Server established!!\n')
s.listen()
usernames = []
clients = []
def receive_message(client):
while True:
try:
# temp = client.recv(1).decode(format)
# msg = temp
# while temp[-1] != '\n':
# temp = client.recv(1).decode(format)
# msg += temp
msg = client.recv(4096).decode(format)
header = msg.split(' ', 1)[0]
for c in clients:
if c != client:
try:
i = clients.index(c)
# print (usernames[i])
c.sendall("".encode(format))
except:
c.close()
# print("hello")
i = clients.index(c)
del usernames[i]
clients.remove(c)
if header == 'WHO\n':
# print(f"Send the member list to {client}")
list = ', '.join(usernames)
list = list + '\n'
reply = 'WHO-OK ' + list
client.send(reply.encode(format))
elif header == 'SEND':
msg_parts = msg.split(' ', 2)
dest_user = msg_parts[1]
if dest_user in usernames:
try:
print('Send message to {}'.format(dest_user))
client_index = clients.index(client)
client_username = usernames[client_index]
msg_to_user = 'DELIVERY' + ' ' + client_username + ' ' + msg_parts[2] + '\n'
index = usernames.index(dest_user)
dest_client = clients[index]
dest_client.sendall(msg_to_user.encode(format))
reply = 'SEND-OK\n'
client.sendall(reply.encode(format))
except:
error_msg = 'BAD-RQST-BODY\n'
client.sendall(error_msg.encode(format))
else:
reply = 'UNKNOWN\n'
client.sendall(reply.encode(format))
elif header == 'QUIT\n':
index = clients.index(client)
clients.remove(client)
username = usernames[index]
usernames.remove(username)
print('{} left the room.'.format(username).encode(format))
print('Current member: ' + str(usernames))
else:
client.sendall('BAD-RQST-HDR\n'.encode(format))
except:
pass
def receive():
try:
while True:
client, address = s.accept()
print('Connected with {}'.format(str(address)))
msg = client.recv(1024).decode(format)
if len(usernames) == 60:
print("Over capacity.")
client.sendall('BUSY\n'.encode(format))
else:
header = msg.split(' ', 2)[0]
# msg_body = msg.split(' ', 2)[1]
if header == 'HELLO-FROM':
try:
parts = msg.split(' ', 1)
username = parts[1][:-1]
uniq = True
for i in usernames:
if i == username:
uniq = False
break
if uniq == False:
print('The username sent from the client was not unique.')
client.sendall('IN-USE\n'.encode(format))
elif uniq == True:
print(username)
reply = 'HELLO ' + username + '\n'
client.sendall(reply.encode(format))
usernames.append(username)
clients.append(client)
print('Current member: ' + str(usernames))
except:
error_msg = 'BAD-RQST-BODY\n'
client.send(error_msg.encode(format))
else:
error_msg = 'BAD-RQST-HDR\n'
client.send(error_msg.encode(format))
thread = threading.Thread(target=receive_message, args=(client,))
thread.start()
except Exception:
s.close()
receive()
# thread = threading.Thread(target=receive)
# thread.start()