-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
executable file
·205 lines (170 loc) · 6.2 KB
/
server.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
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# encoding: utf-8
#
# jpbarraca@ua.pt
# jmr@ua.pt 2016
# vim setings:
# :set expandtab ts=4
from socket import *
from select import *
import json
import sys
import time
import logging
from log import *
from server_client import *
from server_registry import *
from server_actions import *
# Server address
HOST = "" # All available interfaces
PORT = 8080 # The server port
BUFSIZE = 512 * 1024
TERMINATOR = "\n\n"
MAX_BUFSIZE = 64 * 1024
class Server:
def __init__(self, host, port):
self.ss = socket(AF_INET, SOCK_STREAM) # the server socket (IP \ TCP)
self.ss.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
self.ss.bind((host, port))
self.ss.listen(10)
log(logging.INFO, "Secure IM server listening on %s" %
str(self.ss.getsockname()))
self.registry = ServerRegistry()
self.server_actions = ServerActions()
# clients to manage (indexed by socket and by name):
self.clients = {} # clients (key is socket)
def stop(self):
""" Stops the server closing all sockets
"""
log(logging.INFO, "Stopping Server")
try:
self.ss.close()
except:
logging.exception("Server.stop")
for csock in self.clients:
try:
self.clients[csock].close() # Client.close!
except:
# this should not happen since close is protected...
logging.exception("clients[csock].close")
# If we delClient instead, the following would be unnecessary...
self.clients.clear()
def addClient(self, csock, addr):
"""Add a client connecting in csock."""
if csock in self.clients:
log(logging.ERROR, "Client NOT Added: %s already exists" %
self.clients[csock])
return
client = Client(csock, addr)
self.clients[client.socket] = client
log(logging.DEBUG, "Client added: %s" % client)
def delClient(self, csock):
"""Delete a client connected in csock."""
if csock not in self.clients:
log(logging.ERROR, "Client NOT deleted: %s not found" %
self.clients[csock])
return
client = self.clients[csock]
del self.clients[client.socket]
client.close()
log(logging.DEBUG, "Client deleted: %s" % client)
def accept(self):
"""Accept a new connection.
"""
try:
csock, addr = self.ss.accept()
self.addClient(csock, addr)
except:
logging.exception("Could not accept client")
def flushin(self, s):
"""Read a chunk of data from this client.
Enqueue any complete requests.
Leave incomplete requests in buffer.
This is called whenever data is available from client socket.
"""
client = self.clients[s]
data = None
try:
data = s.recv(BUFSIZE)
#log(logging.DEBUG,
# "Received data from %s. Message:\n%r" % (client, data))
except:
#logging.exception("flushin: recv(%s)" % client)
self.delClient(s)
else:
if len(data) > 0:
#log(logging.DEBUG, "PARSIIIING")
reqs = client.parseReqs(data)
for req in reqs:
#log(logging.DEBUG, "HANDLEE")
self.server_actions.handleRequest(s, req, self.clients[s])
else:
self.delClient(s)
def flushout(self, s):
"""Write a chunk of data to client.
This is called whenever client socket is ready to transmit data."""
if s not in self.clients:
return
client = self.clients[s]
try:
sent = client.socket.send(client.bufout[:BUFSIZE])
#log(logging.DEBUG, "Sent %d bytes to %s. Message:\n%r" %
# (sent, client, client.bufout[:sent]))
# leave remaining to be sent later
client.bufout = client.bufout[sent:]
except:
logging.exception("flushout: send(%s)", client)
# logging.error("Cannot write to client %s. Closing", client)
self.delClient(client.socket)
def loop(self):
while True:
# sockets to select for reading: (the server socket + every open
# client connection)
rlist = [self.ss] + self.clients.keys()
# sockets to select for writing: (those that have something in
# bufout)
wlist = [sock for sock in self.clients if len(
self.clients[sock].bufout) > 0]
(rl, wl, xl) = select(rlist, wlist, rlist)
# Deal with incoming data:
for s in rl:
if s is self.ss:
self.accept()
elif s in self.clients:
self.flushin(s)
else:
log(logging.ERROR,
"Incoming, but %s not in clients anymore" % s)
# Deal with outgoing data:
for s in wl:
if s in self.clients:
self.flushout(s)
else:
log(logging.ERROR,
"Outgoing, but %s not in clients anymore" % s)
for s in xl:
log(logging.ERROR, "EXCEPTION in %s. Closing" % s)
self.delClient(s)
if __name__ == "__main__":
if len(sys.argv) > 1:
PORT = int(sys.argv[1])
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG, formatter=logging.Formatter(
'%(asctime)s - %(levelname)s - %(message)s'))
serv = None
while True:
try:
log(logging.INFO, "Starting Secure IM Server v1.0")
serv = Server(HOST, PORT)
serv.loop()
except KeyboardInterrupt:
serv.stop()
try:
log(logging.INFO, "Press CTRL-C again within 2 sec to quit")
time.sleep(2)
except KeyboardInterrupt:
log(logging.INFO, "CTRL-C pressed twice: Quitting!")
break
except:
logging.exception("Server ERROR")
if serv is not (None):
serv.stop()
time.sleep(10)