-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathecho_select.py
More file actions
172 lines (154 loc) · 6.99 KB
/
echo_select.py
File metadata and controls
172 lines (154 loc) · 6.99 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import socket
import sys
import select
# GLOBAL VARS
max_buff_len = 1500
port = 7777
class Client:
"""
class Client of tcp chat server
socket the socket connection with client
nick the nickname of the client
"""
def __init__(self, socket, nick):
"""
init CLient object
:param socket: the socket connection with client
:param nick: the nickname of the client, (default = socket addr:port)
"""
self.socket = socket
if nick:
self.nick = nick
else:
self.nick = str(socket.getpeername())
def getsocket(self):
return self.socket
def getnick(self):
return str(self.nick)
def setsocket(self, socket):
self.socket = socket
def setnick(self, nick):
self.nick = nick
def connect():
pass
def disconnect(client, all_clients, msg, broadcast):
"""
disconnect a client from the server and send a message to all other clients
:param client: the client to disconnect
:param all_clients: list of all clients on server
:param msg: array containing all words of the message
:param broadcast: specify if msg will be send to all client or not
:return: list without the disconnected Client object
"""
print("{} disconnected".format(client.getsocket().getpeername()))
if broadcast:
for oc in all_clients:
if oc.getsocket() != all_clients[0].getsocket() and oc != client:
if len(msg) == 0:
oc.getsocket().sendall("client \"{}\" disconnected\n".format(client.getnick()).encode("utf-8"))
else:
oc.getsocket().sendall("[{}] {}\n".format(client.getnick(), msg).encode("utf-8"))
else:
if len(msg) == 0:
client.getsocket().sendall(
"You have been disconnected from the server\n".encode("utf-8"))
else:
client.getsocket().sendall("{}\n".format(msg).encode("utf-8"))
all_clients.remove(client)
client.getsocket().close()
print("client disconnected \"{}\"".format(client.getnick()))
return all_clients
def tcp_serv(port):
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
try:
server.bind(('', port))
except Exception as e:
print(e)
exit(1)
inputs = [Client(server, "server")]
outputs = []
server.listen(10)
try:
while True:
r_list, w_list, x_list = select.select([x.getsocket() for x in inputs], [], [])
for t in r_list:
current_client = None
for cs in inputs:
if cs.getsocket() is t:
current_client = cs
if t is server:
client, addr = t.accept()
print("client connected {}".format(addr))
client.setblocking(0)
c = Client(client, addr)
inputs.append(c)
else:
payload = t.recv(max_buff_len)
if payload != b"":
cmd = payload.decode().split()
try:
data = ""
# command match cases
if cmd[0].upper() == "MSG":
# send message to all sockets except socket source
for oc in inputs:
# concatenate all msg and send data
if oc.getsocket() != server and oc.getsocket() != t:
for i in range(len(cmd)):
if i != 0:
if i == 1:
# put name at line beginning
data = "[{}] ".format(current_client.getnick())
data = data + cmd[i]
else:
data = data + " " + cmd[i]
data = data + "\n"
oc.socket.sendall(data.encode("utf-8"))
elif cmd[0].upper() == "NICK":
for i in range(len(cmd)):
if i != 0:
if i == 1:
data = cmd[i]
else:
data = data + " " + cmd[i]
current_client.setnick(data)
print("client \"{}\" => {}".format(current_client.getsocket().getpeername(), current_client.getnick()))
elif cmd[0].upper() == "WHO":
data = "[server] "
for oc in inputs:
if oc.getsocket() is not server:
data += oc.getnick() + " "
data += "\n"
t.sendall(data.encode("utf-8"))
elif cmd[0].upper() == "QUIT":
msg = ""
for i in range(len(cmd)):
if i != 0:
msg += " " + cmd[i]
inputs = disconnect(current_client, inputs, msg, True)
elif cmd[0].upper() == "KILL":
if len(cmd) >= 3:
msg = "[{}] ".format(current_client.getnick())
for i in range(len(cmd)):
if i > 1:
if i != len(cmd) - 1:
msg += cmd[i] + " "
else:
msg += cmd[i]
for oc in inputs:
if oc.getnick() == cmd[1]:
ctodisc = oc
inputs = disconnect(ctodisc, inputs, msg, False)
else:
current_client.getsocket().sendall(
"Error: command bad usage\nUsage: KILL <nick> <message>\n".encode("utf-8"))
else:
t.sendall("Invalid command\n".encode("utf-8"))
except IndexError:
continue
else:
inputs = disconnect(current_client, inputs, "", True)
except KeyboardInterrupt:
exit(1)
tcp_serv(port)