mirror of https://github.com/fail2ban/fail2ban
ENH: Clarify use of bytes in csocket and asyncserver for python3
parent
a8f2e02eea
commit
4db77ebf41
|
@ -31,6 +31,13 @@ __license__ = "GPL"
|
||||||
from pickle import dumps, loads, HIGHEST_PROTOCOL
|
from pickle import dumps, loads, HIGHEST_PROTOCOL
|
||||||
import socket, sys
|
import socket, sys
|
||||||
|
|
||||||
|
if sys.version_info >= (3,):
|
||||||
|
# b"" causes SyntaxError in python <= 2.5, so below implements equivalent
|
||||||
|
EMPTY_BYTES = bytes("", encoding="ascii")
|
||||||
|
else:
|
||||||
|
# python 2.x, string type is equivalent to bytes.
|
||||||
|
EMPTY_BYTES = ""
|
||||||
|
|
||||||
class CSocket:
|
class CSocket:
|
||||||
|
|
||||||
if sys.version_info >= (3,):
|
if sys.version_info >= (3,):
|
||||||
|
@ -55,10 +62,7 @@ class CSocket:
|
||||||
|
|
||||||
#@staticmethod
|
#@staticmethod
|
||||||
def receive(sock):
|
def receive(sock):
|
||||||
if sys.version_info >= (3,):
|
msg = EMPTY_BYTES
|
||||||
msg = bytes("", encoding='ascii')
|
|
||||||
else:
|
|
||||||
msg = ''
|
|
||||||
while msg.rfind(CSocket.END_STRING) == -1:
|
while msg.rfind(CSocket.END_STRING) == -1:
|
||||||
chunk = sock.recv(6)
|
chunk = sock.recv(6)
|
||||||
if chunk == '':
|
if chunk == '':
|
||||||
|
|
|
@ -35,6 +35,13 @@ from fail2ban import helpers
|
||||||
# Gets the instance of the logger.
|
# Gets the instance of the logger.
|
||||||
logSys = logging.getLogger(__name__)
|
logSys = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
if sys.version_info >= (3,):
|
||||||
|
# b"" causes SyntaxError in python <= 2.5, so below implements equivalent
|
||||||
|
EMPTY_BYTES = bytes("", encoding="ascii")
|
||||||
|
else:
|
||||||
|
# python 2.x, string type is equivalent to bytes.
|
||||||
|
EMPTY_BYTES = ""
|
||||||
|
|
||||||
##
|
##
|
||||||
# Request handler class.
|
# Request handler class.
|
||||||
#
|
#
|
||||||
|
@ -66,10 +73,7 @@ class RequestHandler(asynchat.async_chat):
|
||||||
|
|
||||||
def found_terminator(self):
|
def found_terminator(self):
|
||||||
# Joins the buffer items.
|
# Joins the buffer items.
|
||||||
if sys.version_info >= (3,):
|
message = loads(EMPTY_BYTES.join(self.__buffer))
|
||||||
message = loads(bytes("", encoding="ascii").join(self.__buffer))
|
|
||||||
else:
|
|
||||||
message = loads("".join(self.__buffer))
|
|
||||||
# Gives the message to the transmitter.
|
# Gives the message to the transmitter.
|
||||||
message = self.__transmitter.proceed(message)
|
message = self.__transmitter.proceed(message)
|
||||||
# Serializes the response.
|
# Serializes the response.
|
||||||
|
|
Loading…
Reference in New Issue