forked from Krautspace/doorstatus
verarbeitung der statusdaten umgestellt, code verschlankt
statusdaten werden jetzt mit den funktionen encode() und decode() verarbeitet, antwort des servers als variable, finaly klausel wieder entfernt
This commit is contained in:
parent
9894af021e
commit
5f3bb44c7b
2 changed files with 20 additions and 39 deletions
|
@ -1,6 +1,6 @@
|
|||
#!/usr/bin/python3
|
||||
|
||||
# file: statusd.py
|
||||
# file: apistatusd.py
|
||||
# date: 26.07.2019
|
||||
# email: berhsi@web.de
|
||||
|
||||
|
@ -76,13 +76,12 @@ def display_peercert(cert):
|
|||
def receive_buffer_is_valid(raw_data):
|
||||
'''
|
||||
Checks validity of the received buffer contents.
|
||||
param 1: byte
|
||||
param 1: byte object
|
||||
return: boolean
|
||||
'''
|
||||
if raw_data in (b'\x00', b'\x01'):
|
||||
if raw_data.decode('utf-8', 'strict') in ('0', '1'):
|
||||
logging.debug('Argument is valid: {}'.format(raw_data))
|
||||
return True
|
||||
|
||||
logging.debug('Argument is not valid: {}'.format(raw_data))
|
||||
return False
|
||||
|
||||
|
@ -90,7 +89,7 @@ def receive_buffer_is_valid(raw_data):
|
|||
def change_status(raw_data, api):
|
||||
'''
|
||||
Write the new status together with a timestamp into the Space API JSON.
|
||||
param 1: byte
|
||||
param 1: byte object
|
||||
param 2: string
|
||||
return: boolean
|
||||
'''
|
||||
|
@ -113,6 +112,7 @@ def change_status(raw_data, api):
|
|||
except Exception as e:
|
||||
logging.error('Failed to change API file')
|
||||
logging.error('{}'.format(e))
|
||||
return False
|
||||
logging.debug('API file changed')
|
||||
else:
|
||||
logging.error('API file is not writable. Wrong permissions?')
|
||||
|
@ -152,10 +152,10 @@ def set_values(raw_data):
|
|||
'''
|
||||
Create a timestamp, changes the value of the given byte into a string
|
||||
and returns both.
|
||||
param 1: byte
|
||||
param 1: byte object
|
||||
return: tuple
|
||||
'''
|
||||
status = True if raw_data == b'\x01' else False
|
||||
status = True if raw_data.decode('utf-8', 'strict') == '1' else False
|
||||
timestamp = int(str(time()).split('.')[0])
|
||||
|
||||
logging.debug('Set values for timestamp: {} and status: {}'.format(
|
||||
|
@ -173,6 +173,7 @@ def main():
|
|||
(cve-2011-3389)
|
||||
'''
|
||||
|
||||
answer = '3'.encode(encoding='utf-8', errors='strict')
|
||||
loglevel = logging.WARNING
|
||||
formatstring = '%(asctime)s: %(levelname)s: %(message)s'
|
||||
logging.basicConfig(format=formatstring, level=loglevel)
|
||||
|
@ -196,7 +197,7 @@ def main():
|
|||
'template': './api_template'
|
||||
}
|
||||
}
|
||||
configfile = './statusd.conf'
|
||||
configfile = './apistatusd.conf'
|
||||
config = configparser.ConfigParser()
|
||||
config.read_dict(default_config)
|
||||
if not config.read(configfile):
|
||||
|
@ -273,19 +274,10 @@ def main():
|
|||
raw_data = conn.recv(1)
|
||||
if receive_buffer_is_valid(raw_data) is True:
|
||||
if change_status(raw_data, config['api']['api']) is True:
|
||||
logging.debug('Send {} back'.format(raw_data))
|
||||
conn.send(raw_data)
|
||||
# change_status returns false:
|
||||
else:
|
||||
logging.info('Failed to change status')
|
||||
if conn:
|
||||
conn.send(b'\x03')
|
||||
# receive_handle returns false:
|
||||
else:
|
||||
logging.info('Invalid argument received: {}'.format(raw_data))
|
||||
logging.debug('Send {} back'.format(b'\x03'))
|
||||
if conn:
|
||||
conn.send(b'\x03')
|
||||
answer = raw_data
|
||||
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')
|
||||
|
@ -293,13 +285,6 @@ def main():
|
|||
except Exception as e:
|
||||
logging.error('{}'.format(e))
|
||||
continue
|
||||
finally:
|
||||
if mySocket:
|
||||
logging.info('Shutdown socket')
|
||||
try:
|
||||
mySocket.shutdown(socket.SHUT_RDWR)
|
||||
except Exception as e:
|
||||
logging.error(f'Error while shutdown socket: {e}')
|
||||
return 0
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue