Pylint check and clean

This commit is contained in:
Fred Z 2018-03-12 17:51:21 +01:00
parent a14ef683cb
commit 851b618ff7
1 changed files with 20 additions and 19 deletions

View File

@ -50,7 +50,7 @@ class ConnectSocket:
_MSG_USER_IN = "<entered the network>" _MSG_USER_IN = "<entered the network>"
_MSG_WELCOME = "Welcome. First do something usefull and type your name: " _MSG_WELCOME = "Welcome. First do something usefull and type your name: "
_MSG_UNWELCOME = "Sorry, no more place here.\n" _MSG_UNWELCOME = "Sorry, no more place here.\n"
_MSG_UNSUPPORTED = "Unsupported data:«{}»" _MSG_UNSUPPORTED = "Unsupported data:«{}»{}"
_SERVER_LOG = "{}:{}|{name}|{msg}" _SERVER_LOG = "{}:{}|{name}|{msg}"
# Others const # Others const
@ -67,14 +67,14 @@ class ConnectSocket:
""" """
# Setting up the connection # Setting up the connection
self._CONNECTION = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self._main_sckt = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self._CONNECTION.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) self._main_sckt.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self._CONNECTION.bind((host, port)) self._main_sckt.bind((host, port))
self._CONNECTION.listen(5) self._main_sckt.listen(5)
# Init connection list # Init connection list
self._inputs = [] self._inputs = []
self._inputs.append(self._CONNECTION) self._inputs.append(self._main_sckt)
# Init username list, to keep match between inputs & name lists # Init username list, to keep match between inputs & name lists
self._user_name = [] self._user_name = []
@ -104,26 +104,26 @@ class ConnectSocket:
for sckt in recipients: for sckt in recipients:
if sckt != sender: if sckt != sender:
try: sckt.send(message.encode())
sckt.send(message.encode()) # NOTE I remove a `try/except` here because I do not
except: # encountered any Error, I cannot choose the correct
sckt.close() # exception type
self._inputs.remove(sckt) # sckt.close() \ self._inputs.remove(sckt)
def close(self): def close(self):
""" Cleanly closes each socket (clients) of the network """ """ Cleanly closes each socket (clients) of the network """
self._CONNECTION = self._inputs.pop(0) self._main_sckt = self._inputs.pop(0)
i = 1 i = 1
for sckt in self._inputs: for sckt in self._inputs:
self.server_log(self._SERVER_LOG.format( self.server_log(self._SERVER_LOG.format(
*sckt.getpeername(), *sckt.getpeername(),
name=self._user_name[i], name=self._user_name[i],
msg="closed client socket") msg="closed client socket")
) )
sckt.close() sckt.close()
i += 1 i += 1
self._inputs.clear() self._inputs.clear()
self._CONNECTION.close() self._main_sckt.close()
self.server_log(self._MSG_SERVER_STOP) self.server_log(self._MSG_SERVER_STOP)
def count_clients(self): def count_clients(self):
@ -149,11 +149,11 @@ class ConnectSocket:
else: else:
client_list = [ client_list = [
sckt for (idx, sckt) in enumerate(self._inputs) sckt for (idx, sckt) in enumerate(self._inputs)
if sckt != self._CONNECTION if sckt != self._main_sckt
and self._user_name[idx] is not False and self._user_name[idx] is not False
] ]
# FIXME maybe there is a better way for the next condition: when # NOTE maybe there is a better way for the next condition: when
# client connects it has not yet his name filled in the # client connects it has not yet his name filled in the
# _user_name attribut, then this method returns only clients # _user_name attribut, then this method returns only clients
# with a filled name # with a filled name
@ -191,7 +191,7 @@ class ConnectSocket:
sending = True sending = True
# Listen for new client connection # Listen for new client connection
if sckt == self._CONNECTION: if sckt == self._main_sckt:
sckt_object, sckt_addr = sckt.accept() sckt_object, sckt_addr = sckt.accept()
# Maximum connection number is not reached: accepting # Maximum connection number is not reached: accepting
@ -284,7 +284,7 @@ class ConnectSocket:
msg=self._MSG_UNSUPPORTED.format(data) msg=self._MSG_UNSUPPORTED.format(data)
) )
send_msg = self._MSG_UNSUPPORTED+"\n".format(data) send_msg = self._MSG_UNSUPPORTED.format(data, "\n")
broadcasting = False broadcasting = False
@ -297,7 +297,8 @@ class ConnectSocket:
if sending: if sending:
sckt.send(send_msg.encode()) sckt.send(send_msg.encode())
def server_log(self, msg): @staticmethod
def server_log(msg):
""" Log activity on server-side""" """ Log activity on server-side"""
print(msg) print(msg)
# writes in a logfile here TODO19 # writes in a logfile here TODO19