schweren fehler in main() beseitigt, finally klausel hinzu, SO_REUSEADDR hinzu
This commit is contained in:
parent
ef3981fe66
commit
7dd6dbab12
1 changed files with 41 additions and 35 deletions
|
@ -332,9 +332,10 @@ def main():
|
|||
print_context(context)
|
||||
|
||||
try:
|
||||
# normalen socket öffnen => MySocket
|
||||
# tcp socket öffnen => 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)
|
||||
keep = MySocket.getsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE)
|
||||
logging.debug('Socket keepalive: {}'.format(keep))
|
||||
|
@ -347,23 +348,23 @@ def main():
|
|||
logging.error('Unable to bind and listen')
|
||||
logging.error('{}'.format(e))
|
||||
sys.exit(1)
|
||||
# den socket in den ssl-context einwickeln => MySecSocket
|
||||
with context.wrap_socket(MySocket, server_side=True) as MySecSocket:
|
||||
# endlos auf verbindungen warten => ClientSocket
|
||||
while True:
|
||||
connection, remote = MySecSocket.accept()
|
||||
logging.info('Client connected: {}:{}'.format(remote[0], remote[1]))
|
||||
connection.settimeout(float(config['general']['timeout']))
|
||||
# die bestehende connection
|
||||
with connection:
|
||||
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:
|
||||
logging.info('SSL Connection established')
|
||||
try:
|
||||
cert = connection.getpeercert(binary_form=False)
|
||||
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 ValueError:
|
||||
logging.debug('SSL handshake has not been done yet')
|
||||
except Exception as e:
|
||||
logging.debug('Unexpected error: {}'.format(e))
|
||||
|
||||
raw_data = connection.recv(1)
|
||||
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:
|
||||
|
@ -379,12 +380,17 @@ def main():
|
|||
logging.debug('Exception: {}'.format(ex))
|
||||
else: logging.debug('Toot is set to false')
|
||||
logging.debug('Send {} back'.format(raw_data))
|
||||
connection.send(answer)
|
||||
Connection.send(answer)
|
||||
Connection.close()
|
||||
except KeyboardInterrupt:
|
||||
logging.info('Keyboard interrupt received')
|
||||
sys.exit(1)
|
||||
except Exception as e:
|
||||
logging.error('{}'.format(e))
|
||||
finally:
|
||||
if MySocket:
|
||||
MySocket.close()
|
||||
logging.debug('TCP socket closed')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
|
Loading…
Reference in a new issue