forked from Krautspace/doorstatus
funktion main() ueberarbeitet
This commit is contained in:
parent
c7bd0eafff
commit
ef3981fe66
1 changed files with 51 additions and 67 deletions
|
@ -331,76 +331,60 @@ 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:
|
||||||
logging.debug('Socket created')
|
# normalen socket öffnen => MySocket
|
||||||
mySocket.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
|
with socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0) as MySocket:
|
||||||
keep = mySocket.getsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE)
|
logging.debug('Socket created')
|
||||||
logging.debug('Socket keepalive: {}'.format(keep))
|
MySocket.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
|
||||||
try:
|
keep = MySocket.getsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE)
|
||||||
mySocket.bind((config['server']['host'], int(config['server']['port'])))
|
logging.debug('Socket keepalive: {}'.format(keep))
|
||||||
mySocket.listen(5)
|
|
||||||
except Exception as e:
|
|
||||||
logging.error('Unable to bind and listen')
|
|
||||||
logging.error('{}'.format(e))
|
|
||||||
sys.exit(1)
|
|
||||||
logging.info('Listening on {} at Port {}'.format(config['server']['host'],
|
|
||||||
config['server']['port']))
|
|
||||||
|
|
||||||
while True:
|
|
||||||
try:
|
try:
|
||||||
fromSocket, fromAddr = mySocket.accept()
|
MySocket.bind((config['server']['host'], int(config['server']['port'])))
|
||||||
logging.info('Client connected: {}:{}'.format(fromAddr[0], fromAddr[1]))
|
MySocket.listen(5)
|
||||||
try:
|
logging.info('Listening on {} at Port {}'.format(config['server']['host'],
|
||||||
fromSocket.settimeout(float(config['general']['timeout']))
|
config['server']['port']))
|
||||||
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)
|
|
||||||
except ValueError:
|
|
||||||
logging.debug('SSL handshake has not been done yet')
|
|
||||||
except Exception as e:
|
|
||||||
logging.debug('Unexpected error: {}'.format(e))
|
|
||||||
|
|
||||||
raw_data = conn.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:
|
|
||||||
answer = raw_data
|
|
||||||
if config['mastodon']['send'].lower() == 'true':
|
|
||||||
logging.debug('Toot is set to true')
|
|
||||||
try:
|
|
||||||
toot_thread = Toot(status, timestamp, config)
|
|
||||||
toot_thread.run()
|
|
||||||
except InitException as e:
|
|
||||||
logging.error('InitException: {}'.format(e))
|
|
||||||
except Exception as ex:
|
|
||||||
logging.debug('Exception: {}'.format(ex))
|
|
||||||
else: logging.debug('Toot is set to false')
|
|
||||||
if conn:
|
|
||||||
logging.debug('Send {} back'.format(raw_data))
|
|
||||||
conn.send(answer)
|
|
||||||
sleep(0.1) # protection against dos
|
|
||||||
except KeyboardInterrupt:
|
|
||||||
logging.info('Keyboard interrupt received')
|
|
||||||
sys.exit(1)
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
logging.error('Unable to bind and listen')
|
||||||
logging.error('{}'.format(e))
|
logging.error('{}'.format(e))
|
||||||
continue
|
sys.exit(1)
|
||||||
return 0
|
# den socket in den ssl-context einwickeln => MySecSocket
|
||||||
|
with context.wrap_socket(MySocket, server_side=True) as MySecSocket:
|
||||||
|
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:
|
||||||
|
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)
|
||||||
|
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:
|
||||||
|
answer = raw_data
|
||||||
|
if config['mastodon']['send'].lower() == 'true':
|
||||||
|
logging.debug('Toot is set to true')
|
||||||
|
try:
|
||||||
|
toot_thread = Toot(status, timestamp, config)
|
||||||
|
toot_thread.run()
|
||||||
|
except InitException as e:
|
||||||
|
logging.error('InitException: {}'.format(e))
|
||||||
|
except Exception as ex:
|
||||||
|
logging.debug('Exception: {}'.format(ex))
|
||||||
|
else: logging.debug('Toot is set to false')
|
||||||
|
logging.debug('Send {} back'.format(raw_data))
|
||||||
|
connection.send(answer)
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
logging.info('Keyboard interrupt received')
|
||||||
|
sys.exit(1)
|
||||||
|
except Exception as e:
|
||||||
|
logging.error('{}'.format(e))
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|
Loading…
Reference in a new issue