forked from Krautspace/doorstatus
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)
|
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,23 +348,23 @@ 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:
|
||||||
connection, remote = MySecSocket.accept()
|
ClientSocket, ClientAddress = MySocket.accept()
|
||||||
logging.info('Client connected: {}:{}'.format(remote[0], remote[1]))
|
logging.info('Client connected: {}:{}'.format(ClientAddress[0], ClientAddress[1]))
|
||||||
connection.settimeout(float(config['general']['timeout']))
|
# die verbindung in den ssl-context verpacken => Connection
|
||||||
# die bestehende connection
|
with context.wrap_socket(ClientSocket, server_side=True) as Connection:
|
||||||
with connection:
|
logging.info('SSL Connection established')
|
||||||
try:
|
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)
|
display_peercert(cert)
|
||||||
except ValueError:
|
|
||||||
logging.debug('SSL handshake has not been done yet')
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logging.debug('Unexpected error: {}'.format(e))
|
logging.error('Unexpected error: {}'.format(e))
|
||||||
|
continue
|
||||||
raw_data = connection.recv(1)
|
# empfangen und antworten
|
||||||
|
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:
|
||||||
|
@ -379,12 +380,17 @@ def main():
|
||||||
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__':
|
||||||
|
|
Loading…
Reference in a new issue