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
|
#!/usr/bin/python3
|
||||||
|
|
||||||
# file: statusd.py
|
# file: apistatusd.py
|
||||||
# date: 26.07.2019
|
# date: 26.07.2019
|
||||||
# email: berhsi@web.de
|
# email: berhsi@web.de
|
||||||
|
|
||||||
|
@ -76,13 +76,12 @@ def display_peercert(cert):
|
||||||
def receive_buffer_is_valid(raw_data):
|
def receive_buffer_is_valid(raw_data):
|
||||||
'''
|
'''
|
||||||
Checks validity of the received buffer contents.
|
Checks validity of the received buffer contents.
|
||||||
param 1: byte
|
param 1: byte object
|
||||||
return: boolean
|
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))
|
logging.debug('Argument is valid: {}'.format(raw_data))
|
||||||
return True
|
return True
|
||||||
|
|
||||||
logging.debug('Argument is not valid: {}'.format(raw_data))
|
logging.debug('Argument is not valid: {}'.format(raw_data))
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
@ -90,7 +89,7 @@ def receive_buffer_is_valid(raw_data):
|
||||||
def change_status(raw_data, api):
|
def change_status(raw_data, api):
|
||||||
'''
|
'''
|
||||||
Write the new status together with a timestamp into the Space API JSON.
|
Write the new status together with a timestamp into the Space API JSON.
|
||||||
param 1: byte
|
param 1: byte object
|
||||||
param 2: string
|
param 2: string
|
||||||
return: boolean
|
return: boolean
|
||||||
'''
|
'''
|
||||||
|
@ -113,6 +112,7 @@ def change_status(raw_data, api):
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logging.error('Failed to change API file')
|
logging.error('Failed to change API file')
|
||||||
logging.error('{}'.format(e))
|
logging.error('{}'.format(e))
|
||||||
|
return False
|
||||||
logging.debug('API file changed')
|
logging.debug('API file changed')
|
||||||
else:
|
else:
|
||||||
logging.error('API file is not writable. Wrong permissions?')
|
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
|
Create a timestamp, changes the value of the given byte into a string
|
||||||
and returns both.
|
and returns both.
|
||||||
param 1: byte
|
param 1: byte object
|
||||||
return: tuple
|
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])
|
timestamp = int(str(time()).split('.')[0])
|
||||||
|
|
||||||
logging.debug('Set values for timestamp: {} and status: {}'.format(
|
logging.debug('Set values for timestamp: {} and status: {}'.format(
|
||||||
|
@ -173,6 +173,7 @@ def main():
|
||||||
(cve-2011-3389)
|
(cve-2011-3389)
|
||||||
'''
|
'''
|
||||||
|
|
||||||
|
answer = '3'.encode(encoding='utf-8', errors='strict')
|
||||||
loglevel = logging.WARNING
|
loglevel = logging.WARNING
|
||||||
formatstring = '%(asctime)s: %(levelname)s: %(message)s'
|
formatstring = '%(asctime)s: %(levelname)s: %(message)s'
|
||||||
logging.basicConfig(format=formatstring, level=loglevel)
|
logging.basicConfig(format=formatstring, level=loglevel)
|
||||||
|
@ -196,7 +197,7 @@ def main():
|
||||||
'template': './api_template'
|
'template': './api_template'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
configfile = './statusd.conf'
|
configfile = './apistatusd.conf'
|
||||||
config = configparser.ConfigParser()
|
config = configparser.ConfigParser()
|
||||||
config.read_dict(default_config)
|
config.read_dict(default_config)
|
||||||
if not config.read(configfile):
|
if not config.read(configfile):
|
||||||
|
@ -273,19 +274,10 @@ def main():
|
||||||
raw_data = conn.recv(1)
|
raw_data = conn.recv(1)
|
||||||
if receive_buffer_is_valid(raw_data) is True:
|
if receive_buffer_is_valid(raw_data) is True:
|
||||||
if change_status(raw_data, config['api']['api']) is True:
|
if change_status(raw_data, config['api']['api']) is True:
|
||||||
|
answer = raw_data
|
||||||
|
if conn:
|
||||||
logging.debug('Send {} back'.format(raw_data))
|
logging.debug('Send {} back'.format(raw_data))
|
||||||
conn.send(raw_data)
|
conn.send(answer)
|
||||||
# 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')
|
|
||||||
sleep(0.1) # protection against dos
|
sleep(0.1) # protection against dos
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
logging.info('Keyboard interrupt received')
|
logging.info('Keyboard interrupt received')
|
||||||
|
@ -293,13 +285,6 @@ def main():
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logging.error('{}'.format(e))
|
logging.error('{}'.format(e))
|
||||||
continue
|
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
|
return 0
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -60,15 +60,10 @@ class SetStatus:
|
||||||
"""
|
"""
|
||||||
return: boolean
|
return: boolean
|
||||||
"""
|
"""
|
||||||
try:
|
if self.status in ('0', '1'):
|
||||||
self.status = int(self.status)
|
self.log.debug('Set status to {}'.format(self.status))
|
||||||
except Exception as e:
|
|
||||||
self.log.error('Status argument does not represent a integer')
|
|
||||||
return False
|
|
||||||
if self.status in (0, 1):
|
|
||||||
self.log.debug('Set value to {}'.format(self.status))
|
|
||||||
self.status = bytes([self.status])
|
|
||||||
return True
|
return True
|
||||||
|
self.log.debug('{} is not a valid status'.format(self.status))
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def set_config(self):
|
def set_config(self):
|
||||||
|
@ -197,7 +192,6 @@ class SetStatus:
|
||||||
|
|
||||||
# check given status
|
# check given status
|
||||||
if self.check_status() is False:
|
if self.check_status() is False:
|
||||||
self.log.error('No valid status given')
|
|
||||||
exit(1)
|
exit(1)
|
||||||
|
|
||||||
# log config if level is debug
|
# log config if level is debug
|
||||||
|
@ -222,12 +216,14 @@ class SetStatus:
|
||||||
# send status
|
# send status
|
||||||
try:
|
try:
|
||||||
self.log.debug('Send new status: {}'.format(self.status))
|
self.log.debug('Send new status: {}'.format(self.status))
|
||||||
self.connection.send(self.status)
|
self.connection.send(self.status.encode(encoding='utf-8',
|
||||||
|
errors='strict'))
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
self.log.error('Error: {}'.format(e))
|
self.log.error('Error: {}'.format(e))
|
||||||
exit(6)
|
exit(6)
|
||||||
try:
|
try:
|
||||||
response = self.connection.recv(1)
|
response = self.connection.recv(1).decode(encoding='utf-8',
|
||||||
|
errors='strict')
|
||||||
self.log.debug('Server returns: {}'.format(response))
|
self.log.debug('Server returns: {}'.format(response))
|
||||||
if response == self.status:
|
if response == self.status:
|
||||||
self.log.info('Status sucessfull updated')
|
self.log.info('Status sucessfull updated')
|
||||||
|
|
Loading…
Reference in a new issue