obsolete code removed (python <= 2.5) + test case extended

pull/1099/head
sebres 2015-07-06 13:08:13 +02:00
parent 81e659b760
commit 17502bd818
3 changed files with 12 additions and 19 deletions

View File

@ -31,15 +31,9 @@ import sys
class CSocket: class CSocket:
EMPTY_BYTES = "" EMPTY_BYTES = b""
END_STRING = "<F2B_END_COMMAND>" END_STRING = b"<F2B_END_COMMAND>"
CLOSE_STRING = "<F2B_CLOSE_COMMAND>" CLOSE_STRING = b"<F2B_CLOSE_COMMAND>"
# 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')
def __init__(self, sock="/var/run/fail2ban/fail2ban.sock"): def __init__(self, sock="/var/run/fail2ban/fail2ban.sock"):
# Create an INET, STREAMing socket # Create an INET, STREAMing socket

View File

@ -47,14 +47,9 @@ logSys = getLogger(__name__)
class RequestHandler(asynchat.async_chat): class RequestHandler(asynchat.async_chat):
# python 2.x, string type is equivalent to bytes. # python 2.x, string type is equivalent to bytes.
EMPTY_BYTES = "" EMPTY_BYTES = b""
END_STRING = "<F2B_END_COMMAND>" END_STRING = b"<F2B_END_COMMAND>"
CLOSE_STRING = "<F2B_CLOSE_COMMAND>" CLOSE_STRING = b"<F2B_CLOSE_COMMAND>"
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')
def __init__(self, conn, transmitter): def __init__(self, conn, transmitter):
asynchat.async_chat.__init__(self, conn) asynchat.async_chat.__init__(self, conn)
@ -78,9 +73,8 @@ class RequestHandler(asynchat.async_chat):
self.__buffer = [] self.__buffer = []
# Joins the buffer items. # Joins the buffer items.
message = loads(RequestHandler.EMPTY_BYTES.join(buf)) message = loads(RequestHandler.EMPTY_BYTES.join(buf))
# Close if close received # Closes the channel if close was received
if message == RequestHandler.CLOSE_STRING: if message == RequestHandler.CLOSE_STRING:
# Closes the channel.
self.close_when_done() self.close_when_done()
return return
# Gives the message to the transmitter. # Gives the message to the transmitter.

View File

@ -63,6 +63,11 @@ class Socket(unittest.TestCase):
testMessage = ["A", "test", "message"] testMessage = ["A", "test", "message"]
self.assertEqual(client.send(testMessage), testMessage) self.assertEqual(client.send(testMessage), testMessage)
# test close message
client.close()
# 2nd close does nothing
client.close()
self.server.stop() self.server.stop()
serverThread.join(1) serverThread.join(1)
self.assertFalse(os.path.exists(self.sock_name)) self.assertFalse(os.path.exists(self.sock_name))