Skip to content

Commit

Permalink
user auth added
Browse files Browse the repository at this point in the history
  • Loading branch information
rundekugel committed Jul 29, 2022
1 parent 3ad83e8 commit d995aca
Showing 1 changed file with 24 additions and 3 deletions.
27 changes: 24 additions & 3 deletions src/subscriberTest.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
#!/usr/bin/env python
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import paho.mqtt.client as mqtt
import time, sys
from getpass import getpass

topic = "#"

def on_connect(client, userdata, flags, rc):
print("Connected with result code " + str(rc))
res=["ok","?","?","?","?","Auth error"]
print("Connected with result code " + str(rc) +" "+str(res[rc]))
client.subscribe(topic)

def on_message(client, userdata, msg):
if sys.version_info[0]==3:
msg.payload = msg.payload.decode()
print(msg.topic + " " + str(msg.payload))


Expand All @@ -19,15 +25,30 @@ def on_message(client, userdata, msg):
sys.exit()
server=av[1]
port=1883
user=None
passwd=None

if ":" in server:
sp=server.split(":")
server=sp[0]
port=sp[1]
if len(av)>2:
topic=av[2]

for p in av[3:]:
if p[:2]=="-u":
user = p[3:]
if p[:3]=="-pw":
passwd = p[4:]

print("Server: %s:%d Topic: %s"%(server, port, topic))

print("Server: %s:%d Topic: %s User: %s"%(server, port, topic, user))
client = mqtt.Client()
if user:
if not passwd:
passwd = getpass("Password for "+user+": ")
client.username_pw_set(user, passwd)

client.on_connect = on_connect
client.on_message = on_message
client.connect(server, port, 60)
Expand Down

0 comments on commit d995aca

Please sign in to comment.