funktion main() ueberarbeitet
This commit is contained in:
parent
c7bd0eafff
commit
ef3981fe66
1 changed files with 51 additions and 67 deletions
|
@ -331,51 +331,39 @@ def main():
|
||||||
logging.debug('SSL context created')
|
logging.debug('SSL context created')
|
||||||
print_context(context)
|
print_context(context)
|
||||||
|
|
||||||
with socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0) as mySocket:
|
try:
|
||||||
|
# normalen socket öffnen => MySocket
|
||||||
|
with socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0) as MySocket:
|
||||||
logging.debug('Socket created')
|
logging.debug('Socket created')
|
||||||
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))
|
||||||
try:
|
try:
|
||||||
mySocket.bind((config['server']['host'], int(config['server']['port'])))
|
MySocket.bind((config['server']['host'], int(config['server']['port'])))
|
||||||
mySocket.listen(5)
|
MySocket.listen(5)
|
||||||
|
logging.info('Listening on {} at Port {}'.format(config['server']['host'],
|
||||||
|
config['server']['port']))
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
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)
|
||||||
logging.info('Listening on {} at Port {}'.format(config['server']['host'],
|
# den socket in den ssl-context einwickeln => MySecSocket
|
||||||
config['server']['port']))
|
with context.wrap_socket(MySocket, server_side=True) as MySecSocket:
|
||||||
|
|
||||||
while True:
|
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:
|
||||||
try:
|
try:
|
||||||
fromSocket, fromAddr = mySocket.accept()
|
cert = connection.getpeercert(binary_form=False)
|
||||||
logging.info('Client connected: {}:{}'.format(fromAddr[0], fromAddr[1]))
|
|
||||||
try:
|
|
||||||
fromSocket.settimeout(float(config['general']['timeout']))
|
|
||||||
logging.debug('Connection timeout set to {}'.format(
|
|
||||||
config['general']['timeout']))
|
|
||||||
except Exception:
|
|
||||||
logging.error('Cannot set timeout to {}'.format(
|
|
||||||
config['general']['timeout']))
|
|
||||||
try:
|
|
||||||
conn = context.wrap_socket(fromSocket, server_side=True)
|
|
||||||
conn.settimeout(float(config['general']['timeout']))
|
|
||||||
except socket.timeout:
|
|
||||||
logging.error('Socket timeout')
|
|
||||||
continue
|
|
||||||
except Exception as e:
|
|
||||||
logging.error('Connection failed: {}'.format(e))
|
|
||||||
continue
|
|
||||||
logging.info('Connection established')
|
|
||||||
try:
|
|
||||||
cert = conn.getpeercert(binary_form=False)
|
|
||||||
display_peercert(cert)
|
display_peercert(cert)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
logging.debug('SSL handshake has not been done yet')
|
logging.debug('SSL handshake has not been done yet')
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logging.debug('Unexpected error: {}'.format(e))
|
logging.debug('Unexpected error: {}'.format(e))
|
||||||
|
|
||||||
raw_data = conn.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:
|
||||||
|
@ -390,17 +378,13 @@ def main():
|
||||||
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')
|
||||||
if conn:
|
|
||||||
logging.debug('Send {} back'.format(raw_data))
|
logging.debug('Send {} back'.format(raw_data))
|
||||||
conn.send(answer)
|
connection.send(answer)
|
||||||
sleep(0.1) # protection against dos
|
|
||||||
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))
|
||||||
continue
|
|
||||||
return 0
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|
Loading…
Reference in a new issue