diff options
| author | Matheus <matheus.guedes.mg.m@gmail.com> | 2025-10-10 23:22:52 -0300 |
|---|---|---|
| committer | Matheus <matheus.guedes.mg.m@gmail.com> | 2025-10-10 23:22:52 -0300 |
| commit | a4fec975a665b6ebcb07fd7451e381f49f3c497a (patch) | |
| tree | adce02e32102a13c00ea25dea33ce06059654f74 /mqtt_client/main.py | |
| parent | 22935fae60b6ab2040596d4a78d1812e2a16589e (diff) | |
fix:Não colocar credenciais no código
(-_-)
Diffstat (limited to 'mqtt_client/main.py')
| -rw-r--r-- | mqtt_client/main.py | 11 |
1 files changed, 10 insertions, 1 deletions
diff --git a/mqtt_client/main.py b/mqtt_client/main.py index 78cdca6..80b4da9 100644 --- a/mqtt_client/main.py +++ b/mqtt_client/main.py @@ -2,6 +2,7 @@ import paho.mqtt.client as mqtt from paho.mqtt.enums import MQTTProtocolVersion import json from time import sleep +import os def on_connect(client, userdata, flags, reason_code, properties): print(f"Conectado: {reason_code}") @@ -16,10 +17,18 @@ def on_message(client, userdata, msg): print(f"Unit: {payload["unit"]}") print(f"Timestamp: {payload["timestamp"]}") +try: + user_name = os.environ["MQTT_CLIENT_USER"] + user_pass = os.environ["MQTT_CLIENT_PASSWORD"] +except KeyError: + print("credentials not supplied in environment variables. Going unauthenticated...") + user_name = None + user_pass = None + mqttc = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2, protocol=MQTTProtocolVersion.MQTTv5) mqttc.on_connect = on_connect mqttc.on_message = on_message -mqttc.username_pw_set('tester', 'rosebud') +mqttc.username_pw_set(user_name, user_pass) connected = False |
