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')
|
||||
print_context(context)
|
||||
|
||||
with socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0) as mySocket:
|
||||
logging.debug('Socket created')
|
||||
mySocket.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
|
||||
keep = mySocket.getsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE)
|
||||
logging.debug('Socket keepalive: {}'.format(keep))
|
||||
try:
|
||||
mySocket.bind((config['server']['host'], int(config['server']['port'])))
|
||||
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:
|
||||
# normalen socket öffnen => MySocket
|
||||
with socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0) as MySocket:
|
||||
logging.debug('Socket created')
|
||||
MySocket.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
|
||||
keep = MySocket.getsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE)
|
||||
logging.debug('Socket keepalive: {}'.format(keep))
|
||||
try:
|
||||
fromSocket, fromAddr = mySocket.accept()
|
||||
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)
|
||||
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)
|
||||
MySocket.bind((config['server']['host'], int(config['server']['port'])))
|
||||
MySocket.listen(5)
|
||||
logging.info('Listening on {} at Port {}'.format(config['server']['host'],
|
||||
config['server']['port']))
|
||||
except Exception as e:
|
||||
logging.error('Unable to bind and listen')
|
||||
logging.error('{}'.format(e))
|
||||
continue
|
||||
return 0
|
||||
sys.exit(1)
|
||||
# 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__':
|
||||
|
|
Loading…
Reference in a new issue