schweren fehler in main() beseitigt, finally klausel hinzu, SO_REUSEADDR hinzu

This commit is contained in:
example 2022-07-30 10:05:32 +02:00
parent ef3981fe66
commit 7dd6dbab12

View file

@ -332,9 +332,10 @@ def main():
print_context(context) print_context(context)
try: try:
# normalen socket öffnen => MySocket # tcp socket öffnen => MySocket
with socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0) as MySocket: with socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0) as MySocket:
logging.debug('Socket created') logging.debug('TCP Socket created')
MySocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
MySocket.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1) MySocket.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
keep = MySocket.getsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE) keep = MySocket.getsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE)
logging.debug('Socket keepalive: {}'.format(keep)) logging.debug('Socket keepalive: {}'.format(keep))
@ -347,44 +348,49 @@ def main():
logging.error('Unable to bind and listen') logging.error('Unable to bind and listen')
logging.error('{}'.format(e)) logging.error('{}'.format(e))
sys.exit(1) sys.exit(1)
# den socket in den ssl-context einwickeln => MySecSocket # endlos auf verbindungen warten => ClientSocket
with context.wrap_socket(MySocket, server_side=True) as MySecSocket: while True:
while True: ClientSocket, ClientAddress = MySocket.accept()
connection, remote = MySecSocket.accept() logging.info('Client connected: {}:{}'.format(ClientAddress[0], ClientAddress[1]))
logging.info('Client connected: {}:{}'.format(remote[0], remote[1])) # die verbindung in den ssl-context verpacken => Connection
connection.settimeout(float(config['general']['timeout'])) with context.wrap_socket(ClientSocket, server_side=True) as Connection:
# die bestehende connection logging.info('SSL Connection established')
with connection: try:
try: Connection.settimeout(float(config['general']['timeout']))
cert = connection.getpeercert(binary_form=False) logging.debug('Connection timeout set to {}'.format(config['general']['timeout']))
display_peercert(cert) cert = Connection.getpeercert(binary_form=False)
except ValueError: display_peercert(cert)
logging.debug('SSL handshake has not been done yet') except Exception as e:
except Exception as e: logging.error('Unexpected error: {}'.format(e))
logging.debug('Unexpected error: {}'.format(e)) continue
# 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()
except KeyboardInterrupt: except KeyboardInterrupt:
logging.info('Keyboard interrupt received') logging.info('Keyboard interrupt received')
sys.exit(1) sys.exit(1)
except Exception as e: except Exception as e:
logging.error('{}'.format(e)) logging.error('{}'.format(e))
finally:
if MySocket:
MySocket.close()
logging.debug('TCP socket closed')
if __name__ == '__main__': if __name__ == '__main__':