secure socket wieder in try-except; finally-klauses wieder raus

This commit is contained in:
example 2022-08-03 17:58:27 +02:00
parent 991eeea9f8
commit 6bd34360a6

View file

@ -379,44 +379,47 @@ def main():
ClientSocket, ClientAddress = MySocket.accept()
logging.info('Client connected: {}:{}'.format(ClientAddress[0], ClientAddress[1]))
# die verbindung in den ssl-context verpacken => Connection
with context.wrap_socket(ClientSocket, server_side=True) as Connection:
try:
Connection = context.wrap_socket(ClientSocket, server_side=True)
logging.info('SSL Connection established')
try:
Connection.settimeout(float(config['general']['timeout']))
logging.debug('Connection timeout set to {}'.format(config['general']['timeout']))
cert = Connection.getpeercert(binary_form=False)
display_peercert(cert)
except Exception as e:
logging.error('Unexpected error: {}'.format(e))
continue
# empfangen und antworten
raw_data = Connection.recv(1)
if receive_buffer_is_valid(raw_data) is True:
status, timestamp = get_status_and_time(raw_data)
if change_status(status, timestamp, config['api']['api']) is True:
answer = raw_data
if config['mastodon']['send'].lower() == 'true':
logging.debug('Toot is set to true')
try:
toot_thread = Toot(status, timestamp, config)
toot_thread.run()
except InitException as e:
logging.error('InitException: {}'.format(e))
except Exception as ex:
logging.debug('Exception: {}'.format(ex))
else: logging.debug('Toot is set to false')
logging.debug('Send {} back'.format(raw_data))
Connection.send(answer)
Connection.close()
Connection.settimeout(float(config['general']['timeout']))
logging.debug('Connection timeout set to {}'.format(config['general']['timeout']))
cert = Connection.getpeercert(binary_form=False)
display_peercert(cert)
except Exception as e:
logging.error('Unexpected error: {}'.format(e))
continue
# empfangen und antworten
raw_data = Connection.recv(1)
if receive_buffer_is_valid(raw_data) is True:
status, timestamp = get_status_and_time(raw_data)
if change_status(status, timestamp, config['api']['api']) is True:
answer = raw_data
if config['mastodon']['send'].lower() == 'true':
logging.debug('Toot is set to true')
try:
toot_thread = Toot(status, timestamp, config)
toot_thread.run()
except InitException as e:
logging.error('InitException: {}'.format(e))
except Exception as ex:
logging.debug('Exception: {}'.format(ex))
else: logging.debug('Toot is set to false')
logging.debug('Send {} back'.format(raw_data))
Connection.send(answer)
Connection.close()
except KeyboardInterrupt:
logging.info('Keyboard interrupt received')
sys.exit(255)
except Exception as e:
logging.error('{}'.format(e))
finally:
if MySocket:
MySocket.close()
logging.debug('TCP socket closed')
sys.exit(255)
except Exception as e:
logging.error('{}'.format(e))
if MySocket:
MySocket.close()
logging.debug('TCP socket closed')
sys.exit(254)
if __name__ == '__main__':