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