From 5fe5d2a1c94c9af49082e315411e6e169050d6b8 Mon Sep 17 00:00:00 2001 From: Ahti Liin Date: Tue, 7 May 2024 19:45:45 +0300 Subject: [PATCH] Sanitize hostname by replacing illegal topic characters (#105) Co-authored-by: Ahti Liin --- src/rpi-cpu2mqtt.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/rpi-cpu2mqtt.py b/src/rpi-cpu2mqtt.py index c46a9a6..bc5b3d2 100644 --- a/src/rpi-cpu2mqtt.py +++ b/src/rpi-cpu2mqtt.py @@ -690,7 +690,8 @@ exit_flag = False stop_event = threading.Event() script_dir = os.path.dirname(os.path.realpath(__file__)) # get device host name - used in mqtt topic -hostname = socket.gethostname() +# and adhere to the allowed character set +hostname = re.sub(r'[^a-zA-Z0-9_-]', '_', socket.gethostname()) if __name__ == '__main__': args = parse_arguments();