From 17502bd818a2396702dcf32713f97234da4bdb6d Mon Sep 17 00:00:00 2001 From: sebres Date: Mon, 6 Jul 2015 13:08:13 +0200 Subject: [PATCH] obsolete code removed (python <= 2.5) + test case extended --- fail2ban/client/csocket.py | 12 +++--------- fail2ban/server/asyncserver.py | 14 ++++---------- fail2ban/tests/sockettestcase.py | 5 +++++ 3 files changed, 12 insertions(+), 19 deletions(-) diff --git a/fail2ban/client/csocket.py b/fail2ban/client/csocket.py index 1bb7e7de..171f8339 100644 --- a/fail2ban/client/csocket.py +++ b/fail2ban/client/csocket.py @@ -31,15 +31,9 @@ import sys class CSocket: - EMPTY_BYTES = "" - END_STRING = "" - CLOSE_STRING = "" - # python 2.x, string type is equivalent to bytes. - if sys.version_info >= (3,): - # b"" causes SyntaxError in python <= 2.5, so below implements equivalent - EMPTY_BYTES = bytes(EMPTY_BYTES, encoding="ascii") - END_STRING = bytes(END_STRING, encoding='ascii') - CLOSE_STRING = bytes(CLOSE_STRING, encoding='ascii') + EMPTY_BYTES = b"" + END_STRING = b"" + CLOSE_STRING = b"" def __init__(self, sock="/var/run/fail2ban/fail2ban.sock"): # Create an INET, STREAMing socket diff --git a/fail2ban/server/asyncserver.py b/fail2ban/server/asyncserver.py index 72406afa..29d1ccc0 100644 --- a/fail2ban/server/asyncserver.py +++ b/fail2ban/server/asyncserver.py @@ -47,14 +47,9 @@ logSys = getLogger(__name__) class RequestHandler(asynchat.async_chat): # python 2.x, string type is equivalent to bytes. - EMPTY_BYTES = "" - END_STRING = "" - CLOSE_STRING = "" - if sys.version_info >= (3,): - # b"" causes SyntaxError in python <= 2.5, so below implements equivalent - EMPTY_BYTES = bytes(EMPTY_BYTES, encoding="ascii") - END_STRING = bytes(END_STRING, encoding="ascii") - CLOSE_STRING = bytes(CLOSE_STRING, encoding='ascii') + EMPTY_BYTES = b"" + END_STRING = b"" + CLOSE_STRING = b"" def __init__(self, conn, transmitter): asynchat.async_chat.__init__(self, conn) @@ -78,9 +73,8 @@ class RequestHandler(asynchat.async_chat): self.__buffer = [] # Joins the buffer items. message = loads(RequestHandler.EMPTY_BYTES.join(buf)) - # Close if close received + # Closes the channel if close was received if message == RequestHandler.CLOSE_STRING: - # Closes the channel. self.close_when_done() return # Gives the message to the transmitter. diff --git a/fail2ban/tests/sockettestcase.py b/fail2ban/tests/sockettestcase.py index 01e72847..1606d1a4 100644 --- a/fail2ban/tests/sockettestcase.py +++ b/fail2ban/tests/sockettestcase.py @@ -63,6 +63,11 @@ class Socket(unittest.TestCase): testMessage = ["A", "test", "message"] self.assertEqual(client.send(testMessage), testMessage) + # test close message + client.close() + # 2nd close does nothing + client.close() + self.server.stop() serverThread.join(1) self.assertFalse(os.path.exists(self.sock_name))