Fixing starttls for mailservers

This commit is contained in:
Blallo 2018-07-27 15:12:40 +02:00
parent 508d6f6121
commit 2de3e5c5b9

View file

@ -15,29 +15,26 @@ logging.basicConfig(level=logging.INFO,
logger = logging.getLogger('certo') logger = logging.getLogger('certo')
# The following two functions from # The following inspired by:
# https://stackoverflow.com/questions/17667903/python-socket-receive-large-amount-of-data # https://stackoverflow.com/questions/17667903/python-socket-receive-large-amount-of-data
# https://www.binarytides.com/receive-full-data-with-the-recv-socket-function-in-python/
def recv_msg(sock, timeout): def recv_msg(sock, timeout):
# Read message length and unpack it into an integer # Read message length and unpack it into an integer
raw_msglen = recvall(sock, 1024, timeout)
if not raw_msglen:
return None
msglen = struct.unpack('>I', raw_msglen)[0]
# Read the message data
return recvall(sock, msglen, timeout)
def recvall(sock, n, timeout):
# Helper function to recv n bytes or return None if EOF is hit
data = b'' data = b''
begin = time.time() begin = time.time()
while len(data) < n: n = 1024
packet = sock.recv(n - len(data)) while len(data) < n or time.time() - begin < timeout:
if not packet: try:
return None data += sock.recv(n - len(data))
data += packet logger.debug("Partial: %r" % data)
if time.time() - begin > timeout: except socket.timeout:
pass
if b'Ready to start TLS\r\n' in data:
break break
time.sleep(0.1)
if len(data) == 0:
return None
logger.debug("Data: %r" % data)
return data return data