This repository has been archived on 2021-09-15. You can view files and clone it, but cannot push or open issues or pull requests.
L3DNC/serveur/Server.py

427 lines
16 KiB
Python
Raw Normal View History

2015-04-09 08:12:41 +00:00
import os
import socket
import threading
import sys
import configparser
import re
2015-03-30 18:54:29 +00:00
from serveur import Log
2015-04-15 06:46:33 +00:00
### Code retour ###
2015-04-13 16:31:41 +00:00
### INFO
2015-04-15 06:47:20 +00:00
2015-04-13 11:43:39 +00:00
USERLIST_ENABLE = 300
USERLIST_DISABLE = 301
HAS_JOIN = 302
HAS_LEFT = 303
NEW_MSG = 304
NAME_CHANGED = 305
NEW_PM = 306
ASKING_FOR_PM = 307
PRIVATE_DISCU_ACCEPTED_FROM = 308
PRIVATE_DISCU_REFUSED_FROM = 309
IS_NOW_ENABLE = 310
IS_NOW_DISABLE = 311
HAS_ASKED_FILE = 312
CAN_SEND_FILE = 313
HAS_REJECT_FILE = 314
### SUCCESS
SUCC_CHANNEL_JOINED = 200
SUCC_CHANNEL_QUIT = 201
SUCC_MESSAGE_SENDED = 202
SUCC_NICKNAME_CHANGED = 203
SUCC_PM_SENDED = 205
SUCCESSFUL_ASKED_CONV = 206
SUCCESSFUL_ACCEPTED_CONV = 207
SUCCESSFUL_REFUSED_CONV = 208
SUCC_ENABLED = 209
SUCC_DISABLED = 210
SUCC_PMFILE = 211
SUCC_ACCEPTED_FILE = 212
SUCC_REFUSED_FILE = 213
### ERROR
ERR_NICKNAME_ALREADY_USED = 400
ERR_NO_NICKNAME = 401
ERR_CONV_NOT_ALLOWED = 402
DEST_NOT_FOUND = 403
ERR_ALREADY_ASKED_FOR_PM = 404
ERR_NO_INVIT_TO_CONV_FOUND = 405
ERR_UNKNOWN_ACCEPTED_FILE = 406
COMMAND_NOT_FOUND = 407
ERR_INVALID_NICKNAME = 408
ERR_INTERNAL_SERVER_ERROR = 409
ERR_NOT_DISABLED = 410
ERR_NOT_ENABLED = 411
2015-04-09 08:12:41 +00:00
##
# Handle a connection from a client.
# Wait for request from the client
# @param connection the socket descriptor of the connection
# @param client_adress ("ip", port) of the connection
def handle_connection(connection, client_address):
2015-04-03 09:13:58 +00:00
try:
log.printL("Connection from IP -> {}".format(client_address), Log.lvl.INFO)
while True:
data = connection.recv(4096)
if data:
log.printL("Request from IP -> {}"
2015-04-03 10:09:00 +00:00
" {}".format(client_address, data.decode()), Log.lvl.INFO)
2015-04-09 08:12:41 +00:00
threading.Thread(target=handle_request, args=(connection, data.decode())).start()
2015-04-03 09:13:58 +00:00
else:
break
2015-04-03 10:09:00 +00:00
except Exception as e:
log.printL("Handle connection fail : ".format(str(e)), Log.lvl.FAIL)
2015-04-03 09:13:58 +00:00
finally:
2015-04-09 08:12:41 +00:00
quit_user(connection)
2015-03-30 18:54:29 +00:00
2015-04-09 08:12:41 +00:00
##
# Handle a request.
# @param connection the socket descriptor of the request sender
# @param data the request to handle in String
def handle_request(connection, data):
2015-04-08 19:35:52 +00:00
try:
2015-04-09 08:12:41 +00:00
array_data = data.split(" ")
2015-04-08 19:28:40 +00:00
2015-04-08 19:35:52 +00:00
### Command for user with nickname ###
if usersConnected[connection][1] is not None:
### No command -> new message ###
2015-04-09 08:12:41 +00:00
if not array_data[0][0] == "/" and usersConnected[connection][2]:
2015-04-13 11:43:39 +00:00
connection.sendall("{}".format(SUCC_MESSAGE_SENDED).encode())
broadcast_message(connection, "{} {} {} ".format(NEW_MSG, usersConnected[connection][1], data))
2015-04-08 19:28:40 +00:00
return
2015-04-08 19:35:52 +00:00
else:
### Command for user enable & disable ###
2015-04-09 08:12:41 +00:00
if array_data[0] == "/name":
change_name(connection, array_data[1])
2015-04-08 19:28:40 +00:00
return
2015-04-09 08:12:41 +00:00
if array_data[0] == "/userlist":
user_list_active(connection)
2015-04-08 19:28:40 +00:00
return
2015-04-09 08:12:41 +00:00
if array_data[0] == "/userlistaway":
user_list_away(connection)
2015-04-08 19:28:40 +00:00
return
2015-04-09 08:12:41 +00:00
if array_data[0] == "/enable":
enable_user(connection)
2015-04-08 19:28:40 +00:00
return
2015-04-09 08:12:41 +00:00
if array_data[0] == "/disable":
disable_user(connection)
2015-04-08 19:28:40 +00:00
return
2015-04-09 08:12:41 +00:00
if array_data[0] == "/quit":
2015-04-08 19:35:52 +00:00
connection.shutdown(socket.SHUT_RD)
2015-04-08 19:28:40 +00:00
return
2015-04-08 19:35:52 +00:00
### Command available for enable only ###
2015-04-09 08:12:41 +00:00
if not usersConnected[connection][2]:
2015-04-13 11:43:39 +00:00
connection.sendall("{}".format(ERR_CONV_NOT_ALLOWED).encode())
2015-04-08 19:28:40 +00:00
return
2015-04-09 08:12:41 +00:00
else:
if array_data[0] == "/askpm":
ask_private_message(connection, array_data[1])
2015-04-08 19:35:52 +00:00
return
2015-04-09 08:12:41 +00:00
if array_data[0] == "/acceptpm":
accept_private_message(connection, array_data[1])
2015-04-08 19:35:52 +00:00
return
2015-04-09 08:12:41 +00:00
if array_data[0] == "/rejectpm":
reject_private_message(connection, array_data[1])
2015-04-08 19:35:52 +00:00
return
2015-04-09 08:12:41 +00:00
if array_data[0] == "/pm":
private_message(connection, array_data[1], " ".join(array_data[2:]))
2015-04-08 19:35:52 +00:00
return
2015-04-09 08:12:41 +00:00
if array_data[0] == "/pmfile":
ask_file(connection, array_data[1], array_data[2])
2015-04-08 19:35:52 +00:00
return
2015-04-09 08:12:41 +00:00
if array_data[0] == "/acceptfile":
2015-04-09 13:49:13 +00:00
accept_file(connection, array_data[1], " ".join(array_data[3:]), array_data[2])
2015-04-08 19:35:52 +00:00
return
2015-04-09 08:12:41 +00:00
if array_data[0] == "/rejectfile":
reject_file(connection, array_data[1], " ".join(array_data[2:]))
2015-04-08 19:35:52 +00:00
return
2015-04-13 11:43:39 +00:00
connection.sendall("{}".format(COMMAND_NOT_FOUND).encode())
2015-04-08 19:35:52 +00:00
else:
### Command for user without nickname ###
2015-04-09 08:12:41 +00:00
if array_data[0] == "/newname":
new_name(connection, array_data[1])
2015-04-08 19:35:52 +00:00
return
2015-04-09 08:12:41 +00:00
if array_data[0] == "/quit":
2015-04-08 19:35:52 +00:00
connection.shutdown(socket.SHUT_RD)
return
2015-04-13 11:43:39 +00:00
connection.sendall("{}".format(ERR_NO_NICKNAME).encode())
2015-04-08 19:35:52 +00:00
except IndexError:
2015-04-08 17:16:40 +00:00
log.printL("Parameter missing in the request", Log.lvl.WARNING)
2015-04-13 11:43:39 +00:00
connection.sendall("{}".format(COMMAND_NOT_FOUND).encode())
2015-04-09 08:12:41 +00:00
except Exception as e:
2015-04-03 10:09:00 +00:00
log.printL("Handle request fail : {}".format(str(e)), Log.lvl.FAIL)
2015-04-13 11:43:39 +00:00
connection.sendall("{}".format(ERR_INTERNAL_SERVER_ERROR).encode())
2015-04-03 10:09:00 +00:00
2015-04-09 08:12:41 +00:00
##
# Broadcast a message to all the users connected except to the sender of the request
# @param connection the socket descriptor of the request sender
2015-04-09 13:49:13 +00:00
# @param message message to broadcast (String)
2015-04-09 08:12:41 +00:00
def broadcast_message(connection, message):
2015-04-03 09:19:47 +00:00
log.printL("User Connected : {}".format(usersConnected), Log.lvl.DEBUG)
2015-04-03 10:09:00 +00:00
for con, value in usersConnected.items():
2015-04-09 13:49:13 +00:00
# value 1 : pseudo value 2 : status (enable/disable)
2015-04-09 08:12:41 +00:00
if value[1] is not None and con != connection and value[2]:
2015-04-02 14:53:31 +00:00
try:
2015-04-02 15:45:24 +00:00
con.sendall(message.encode())
2015-04-03 10:09:00 +00:00
except Exception as e:
2015-04-02 14:53:31 +00:00
log.printL(str(e), Log.lvl.FAIL)
2015-03-31 09:53:24 +00:00
2015-04-09 08:12:41 +00:00
##
# Send the list of enable user
# @param connection the socket descriptor of the target
def user_list_active(connection):
2015-04-13 11:43:39 +00:00
l = "{} ".format(USERLIST_ENABLE)
2015-04-03 10:09:00 +00:00
for con, value in usersConnected.items():
if value[1] is not None and value[2]:
2015-03-31 09:53:24 +00:00
l += value[1] + " "
2015-04-02 15:45:24 +00:00
connection.sendall(l[:-1].encode())
2015-03-31 09:53:24 +00:00
2015-04-09 08:12:41 +00:00
##
# Send the list of disable user
# @param connection the socket descriptor of the target
def user_list_away(connection):
2015-04-13 11:43:39 +00:00
l = "{} ".format(USERLIST_DISABLE)
2015-04-03 10:09:00 +00:00
for con, value in usersConnected.items():
if value[1] is not None and not value[2]:
2015-03-31 09:53:24 +00:00
l += value[1] + " "
2015-04-02 15:45:24 +00:00
connection.sendall(l[:-1].encode())
2015-03-30 18:54:29 +00:00
2015-04-09 08:12:41 +00:00
##
# Change the nickname of the user
# @param connection the socket descriptor of the target
# @param pseudo new nickname for the user (String)
def change_name(connection, pseudo):
2015-04-03 10:09:00 +00:00
if not re.match("^\w{3,15}$", pseudo):
2015-04-13 11:43:39 +00:00
connection.sendall("{}".format(ERR_INVALID_NICKNAME).encode())
2015-04-09 08:12:41 +00:00
elif get_connection_by_pseudo(pseudo) is not None:
2015-04-13 11:43:39 +00:00
connection.sendall("{}".format(ERR_NICKNAME_ALREADY_USED).encode())
2015-04-02 14:53:31 +00:00
else:
2015-04-13 11:43:39 +00:00
broadcast_message(connection, "{} {} {}".format(NAME_CHANGED, usersConnected[connection][1], pseudo))
connection.sendall("{}".format(SUCC_NICKNAME_CHANGED).encode())
2015-04-02 14:53:31 +00:00
usersConnected[connection][1] = pseudo
2015-03-30 18:54:29 +00:00
2015-04-09 08:12:41 +00:00
##
# Affect the nickname of the user for the first time
# @param connection the socket descriptor of the targ
# @param pseudo nickname for the user (String)
def new_name(connection, pseudo):
2015-04-03 10:09:00 +00:00
if not re.match("^\w{3,15}$", pseudo):
2015-04-13 11:43:39 +00:00
connection.sendall("{}".format(ERR_INVALID_NICKNAME).encode())
2015-04-09 08:12:41 +00:00
elif get_connection_by_pseudo(pseudo) is not None:
2015-04-13 11:43:39 +00:00
connection.sendall("{}".format(ERR_NICKNAME_ALREADY_USED).encode())
2015-04-02 15:45:24 +00:00
else:
2015-04-13 11:43:39 +00:00
broadcast_message(connection, "{} {} ".format(HAS_JOIN, pseudo))
connection.sendall("{}".format(SUCC_CHANNEL_JOINED).encode())
2015-04-14 11:16:26 +00:00
usersConnected[connection][1] = pseudo
2015-04-02 11:47:02 +00:00
2015-04-09 08:12:41 +00:00
def ask_private_message(connection, pseudo):
c = get_connection_by_pseudo(pseudo)
2015-04-03 10:09:00 +00:00
if c is None:
2015-04-13 11:43:39 +00:00
connection.sendall("{}".format(DEST_NOT_FOUND).encode())
2015-03-31 09:53:24 +00:00
else:
2015-04-03 10:09:00 +00:00
pm = (connection, c)
if pm in askPM:
2015-04-13 11:43:39 +00:00
connection.sendall("{}".format(ERR_ALREADY_ASKED_FOR_PM).encode())
2015-03-31 09:53:24 +00:00
else:
askPM.append(pm)
2015-04-06 12:55:45 +00:00
log.printL("askPm {}".format(askPM), Log.lvl.DEBUG)
2015-04-13 11:43:39 +00:00
c.sendall("{} {}".format(ASKING_FOR_PM, usersConnected[connection][1]).encode())
connection.sendall("{}".format(SUCCESSFUL_ASKED_CONV).encode())
2015-03-31 09:53:24 +00:00
2015-04-09 08:12:41 +00:00
def accept_private_message(connection, pseudo):
2015-04-06 12:55:45 +00:00
log.printL("askPm {}".format(askPM), Log.lvl.DEBUG)
2015-04-09 08:12:41 +00:00
c = get_connection_by_pseudo(pseudo)
2015-04-03 10:09:00 +00:00
if c is None:
2015-04-13 11:43:39 +00:00
connection.sendall("{}".format(DEST_NOT_FOUND).encode())
2015-03-31 09:53:24 +00:00
else:
2015-04-06 12:55:45 +00:00
pm = (c, connection)
2015-04-03 10:09:00 +00:00
if pm not in askPM:
2015-04-13 11:43:39 +00:00
connection.sendall("{}".format(ERR_NO_INVIT_TO_CONV_FOUND).encode())
2015-03-31 09:53:24 +00:00
else:
askPM.remove(pm)
validatePM.append(pm)
2015-04-13 11:43:39 +00:00
connection.sendall("{}".format(SUCCESSFUL_ACCEPTED_CONV).encode())
c.sendall("{} {}".format(PRIVATE_DISCU_ACCEPTED_FROM, usersConnected[connection][1]).encode())
2015-03-31 09:53:24 +00:00
2015-04-09 08:12:41 +00:00
def reject_private_message(connection, pseudo):
c = get_connection_by_pseudo(pseudo)
2015-04-03 10:09:00 +00:00
if c is None:
2015-04-13 11:43:39 +00:00
connection.sendall("{}".format(DEST_NOT_FOUND).encode())
2015-03-31 09:53:24 +00:00
else:
2015-04-06 12:55:45 +00:00
pm = (c, connection)
2015-04-08 08:08:59 +00:00
pmr = (connection, c)
2015-04-03 10:09:00 +00:00
if pm not in askPM:
2015-04-09 08:12:41 +00:00
if pm in validatePM:
2015-04-08 08:08:59 +00:00
validatePM.remove(pm)
2015-04-13 11:43:39 +00:00
connection.sendall("{}".format(SUCCESSFUL_REFUSED_CONV).encode())
c.sendall("{} {}".format(PRIVATE_DISCU_REFUSED_FROM, usersConnected[connection][1]).encode())
2015-04-09 08:12:41 +00:00
elif pmr in validatePM:
2015-04-08 17:16:40 +00:00
validatePM.remove(pmr)
2015-04-13 11:43:39 +00:00
connection.sendall("{}".format(SUCCESSFUL_REFUSED_CONV).encode())
c.sendall("{} {}".format(PRIVATE_DISCU_REFUSED_FROM, usersConnected[connection][1]).encode())
2015-04-09 08:12:41 +00:00
else:
2015-04-13 11:43:39 +00:00
connection.sendall("{}".format(ERR_NO_INVIT_TO_CONV_FOUND).encode())
2015-03-31 09:53:24 +00:00
else:
askPM.remove(pm)
2015-04-13 11:43:39 +00:00
connection.sendall("{}".format(SUCCESSFUL_REFUSED_CONV).encode())
c.sendall("{} {}".format(PRIVATE_DISCU_REFUSED_FROM, usersConnected[connection][1]).encode())
2015-03-31 09:53:24 +00:00
2015-04-09 08:12:41 +00:00
def private_message(connection, pseudo, msg):
c = get_connection_by_pseudo(pseudo)
2015-04-03 10:09:00 +00:00
if c is None:
2015-04-13 11:43:39 +00:00
connection.sendall("{}".format(DEST_NOT_FOUND).encode())
2015-03-31 09:53:24 +00:00
else:
2015-04-03 10:09:00 +00:00
pm = (connection, c)
2015-04-09 08:12:41 +00:00
pmr = (c, connection)
2015-04-07 11:48:21 +00:00
if pm not in validatePM and pmr not in validatePM:
2015-04-13 11:43:39 +00:00
connection.sendall("{}".format(ERR_CONV_NOT_ALLOWED).encode())
2015-03-31 09:53:24 +00:00
else:
2015-04-15 06:46:33 +00:00
c.sendall("{} {} {}".format(NEW_PM,usersConnected[connection][1], msg).encode())
2015-04-13 11:43:39 +00:00
connection.sendall("{}".format(SUCC_PM_SENDED).encode())
2015-03-31 09:53:24 +00:00
2015-04-09 08:12:41 +00:00
def ask_file(connection, pseudo, file):
c = get_connection_by_pseudo(pseudo)
2015-04-06 12:55:45 +00:00
if c is None:
2015-04-13 11:43:39 +00:00
connection.sendall("{}".format(DEST_NOT_FOUND).encode())
2015-04-06 12:55:45 +00:00
else:
f = (connection, c, file)
if f in askFT:
2015-04-13 11:43:39 +00:00
connection.sendall("{}".format(ERR_ALREADY_ASKED_FOR_PM).encode())
2015-04-06 12:55:45 +00:00
else:
askFT.append(f)
2015-04-08 17:16:40 +00:00
log.printL("askFT {}".format(askFT), Log.lvl.DEBUG)
2015-04-13 11:43:39 +00:00
c.sendall("{} {} {}".format(HAS_ASKED_FILE, usersConnected[connection][1], file).encode())
connection.sendall("{}".format(SUCC_PMFILE).encode())
2015-04-06 12:55:45 +00:00
2015-04-09 08:12:41 +00:00
def accept_file(connection, pseudo, file, port):
2015-04-08 17:16:40 +00:00
log.printL("askFT {}".format(askFT), Log.lvl.DEBUG)
2015-04-09 08:12:41 +00:00
c = get_connection_by_pseudo(pseudo)
2015-04-06 12:55:45 +00:00
if c is None:
2015-04-13 11:43:39 +00:00
connection.sendall("{}".format(DEST_NOT_FOUND).encode())
2015-04-06 12:55:45 +00:00
else:
f = (c, connection, file)
if f not in askFT:
2015-04-13 11:43:39 +00:00
connection.sendall("{}".format(ERR_UNKNOWN_ACCEPTED_FILE).encode())
2015-04-06 12:55:45 +00:00
else:
askFT.remove(f)
2015-04-13 18:22:56 +00:00
connection.sendall("{} {}".format(SUCC_ACCEPTED_FILE, usersConnected[c][0][0]).encode())
2015-04-13 11:43:39 +00:00
c.sendall("{} {} {} {} {}".format(CAN_SEND_FILE, pseudo, usersConnected[connection][0][0],
port, file).encode())
2015-04-06 12:55:45 +00:00
2015-04-09 08:12:41 +00:00
def reject_file(connection, pseudo, file):
c = get_connection_by_pseudo(pseudo)
2015-04-06 12:55:45 +00:00
if c is None:
2015-04-13 11:43:39 +00:00
connection.sendall("{}".format(DEST_NOT_FOUND).encode())
2015-04-06 12:55:45 +00:00
else:
f = (c, connection, file)
if f not in askFT:
2015-04-13 11:43:39 +00:00
connection.sendall("{}".format(ERR_UNKNOWN_ACCEPTED_FILE).encode())
2015-04-06 12:55:45 +00:00
else:
askPM.remove(f)
2015-04-13 11:43:39 +00:00
connection.sendall("{}".format(SUCC_REFUSED_FILE).encode())
connection.sendall("{} {} {}".format(HAS_REJECT_FILE, pseudo, file).encode())
2015-04-06 12:55:45 +00:00
2015-04-09 08:12:41 +00:00
def enable_user(connection):
if not usersConnected[connection][2]:
2015-04-01 06:37:05 +00:00
usersConnected[connection][2] = True
2015-04-13 11:43:39 +00:00
connection.sendall("{}".format(SUCC_ENABLED).encode())
broadcast_message(connection, "{} {}".format(IS_NOW_ENABLE, usersConnected[connection][1]))
2015-04-01 06:37:05 +00:00
else:
2015-04-13 11:43:39 +00:00
connection.sendall("{}".format(ERR_NOT_DISABLED).encode())
2015-04-01 06:37:05 +00:00
2015-04-09 08:12:41 +00:00
def disable_user(connection):
if usersConnected[connection][2]:
2015-04-01 06:37:05 +00:00
usersConnected[connection][2] = False
2015-04-13 11:43:39 +00:00
connection.sendall("{}".format(SUCC_DISABLED).encode())
broadcast_message(connection, "{} {}".format(IS_NOW_DISABLE, usersConnected[connection][1]))
2015-04-01 06:37:05 +00:00
else:
2015-04-13 11:43:39 +00:00
connection.sendall("{}".format(ERR_NOT_ENABLED).encode())
2015-04-01 06:37:05 +00:00
2015-04-09 08:12:41 +00:00
def quit_user(connection):
2015-04-03 10:09:00 +00:00
try:
2015-04-13 11:43:39 +00:00
connection.sendall("{}".format(SUCC_CHANNEL_QUIT).encode())
2015-04-03 10:09:00 +00:00
except OSError: # Client close the socket in this side not properly
log.printL("Client IP -> {} close connection not properly"
"".format(usersConnected[connection][0]), Log.lvl.WARNING)
2015-03-30 18:54:29 +00:00
connection.close()
log.printL("Disconnection from IP -> {}".format(usersConnected[connection][0]), Log.lvl.INFO)
2015-04-03 09:13:58 +00:00
pseudo = usersConnected[connection][1]
2015-03-31 11:25:34 +00:00
usersConnected.pop(connection)
2015-04-13 11:43:39 +00:00
broadcast_message(connection, "{} {}".format(HAS_LEFT, pseudo))
2015-03-30 18:54:29 +00:00
2015-03-31 09:53:24 +00:00
2015-04-09 08:12:41 +00:00
def get_connection_by_pseudo(pseudo):
2015-04-03 10:09:00 +00:00
for con, value in usersConnected.items():
if value[1] == pseudo:
2015-03-31 09:53:24 +00:00
return con
return None
2015-03-30 18:54:29 +00:00
def main():
2015-04-03 10:09:00 +00:00
# Global vars
2015-03-31 09:53:24 +00:00
global usersConnected, log, sock
global askPM, validatePM
2015-04-06 12:55:45 +00:00
global askFT
2015-04-03 10:09:00 +00:00
usersConnected = {}
2015-03-31 09:53:24 +00:00
askPM = []
validatePM = []
2015-04-06 12:55:45 +00:00
askFT = []
2015-04-09 08:12:41 +00:00
# Config
2015-04-02 14:53:31 +00:00
config = configparser.ConfigParser()
2015-04-03 10:09:00 +00:00
if not os.path.isfile("dncserver.conf"):
2015-04-02 14:53:31 +00:00
config['NETWORK'] = {'port': '2222'}
config['LOG'] = {'logDirectory': 'log'}
with open('dncserver.conf', 'w') as configfile:
2015-04-03 10:09:00 +00:00
config.write(configfile)
2015-04-02 14:53:31 +00:00
config.read("dncserver.conf")
log = Log.Log(config["LOG"]["logdirectory"])
2015-04-02 15:00:20 +00:00
log.printL("Configuration Log", Log.lvl.INFO)
log.printL("Server start", Log.lvl.INFO)
2015-04-02 14:53:31 +00:00
2015-03-30 18:54:29 +00:00
#Init socket serv
sock = socket.socket()
2015-04-02 14:53:31 +00:00
sock.bind(("", int(config["NETWORK"]["port"])))
2015-03-30 18:54:29 +00:00
sock.listen(5)
2015-04-02 14:53:31 +00:00
log.printL("Server Listen on port {}".format(config["NETWORK"]["port"]), Log.lvl.INFO)
2015-03-30 18:54:29 +00:00
2015-04-03 10:09:00 +00:00
try:
while True:
2015-03-30 18:54:29 +00:00
#Connection client
connection, client_address = sock.accept()
2015-04-03 10:09:00 +00:00
usersConnected[connection] = [client_address, None, True] # ip pseudo status
2015-04-09 08:12:41 +00:00
threading.Thread(target=handle_connection, args=(connection, client_address)).start()
2015-04-03 10:09:00 +00:00
except KeyboardInterrupt:
2015-03-30 18:54:29 +00:00
# Disable to received more requests on socket
for con, value in usersConnected.items():
con.shutdown(socket.SHUT_RD)
2015-04-03 10:09:00 +00:00
finally:
2015-03-30 18:54:29 +00:00
#Wait for threads finish
log.printL("Wait for threads ending", Log.lvl.INFO)
for t in threading.enumerate():
2015-04-03 10:09:00 +00:00
if t != threading.main_thread():
2015-03-30 18:54:29 +00:00
t.join()
sock.close()
log.printL("Server shutdown", Log.lvl.INFO)
sys.exit(0)