在Docker环境中无法识别MQTT用户/密码

5cnsuln7  于 2022-12-11  发布在  Docker
关注(0)|答案(1)|浏览(258)

我正在使用MQTT docker-compose环境,并尝试在订阅者中使用用户名和密码。如果没有密码,发布和订阅都可以正常工作。
我的docker-compose.yml看起来像这样:

version: "3"
services:
  mqtt:
   image: eclipse-mosquitto:latest
   container_name: mqtt
   network_mode: bridge
   ports:
     - 1883:1883
   volumes:
     - ./conf:/mosquitto/config
     - ./log:/mosquitto/log

我在容器上的/mosquito/config/中的mosquito.conf如下所示:

persistence true
persistence_location /mosquitto/data/
log_dest file /mosquitto/log/mosquitto.log
listener 1883
allow_anonymous false
password_file /mosquitto/config/passwd.txt

我的密码文件在/musceto/config/passwd. txt中,其中有一行包含用户名和一个明文密码。

mosquitto_passwd -U /mosquitto/config/passwd.txt

它会对文件进行加密。当我查看passwd.txt时,它有一个带有加密文本的条目:
用户名:=====================
当我运行发布时,它运行得很好。沿着如下:

import paho.mqtt.client as mqtt
import time

def on_publish(client,userdata,result):
   print("[PUB] Data published = %s" % result)

mqtt_broker ="localhost"
client = mqtt.Client("test")
client.on_publish = on_publish
client.connect(mqtt_broker, 1883)

for i in range(10):
   temp = i
   client.publish("Temp", temp)
   print("[PUB] Just published %.4f to topic Temp" % temp)
   time.sleep(1)

然而,订阅永远不会得到它:

import paho.mqtt.client as mqtt
username='user'
password='passwd'

def on_message(client, userdata, message):
   print("[SUB] received message: %s" % message.payload.decode("utf-8"))

def on_connect(client, userdata, flags, rc):
   print("[SUB] Connected with result code "+str(rc))
   client.subscribe("/Temp")

mqtt_broker = "localhost"

client = mqtt.Client("sub")
ttl = 120
client.username_pw_set(username, password)
client.connect(mqtt_broker, 1883, ttl)

client.loop_start()

client.subscribe("Temp")
print("[SUB] Awaiting notifications")
client.loop_forever()

在日志中,我看到未经授权的错误:

1670429277: New connection from 172.17.0.1:59538 on port 1883.
1670429277: Client mac disconnected, not authorised.

可能是什么问题?

iyr7buue

iyr7buue1#

在做了一些调整之后,我已经能够成功地运行您的代码。这可能不是一个完整的答案,因为我还没有能够复制您的问题,但可以为您指出工作的代码。
我在您的代码中看到的主要问题是,您没有配置client以使用回调。例如:

client.on_message = on_message
client.on_connect = on_connect

这意味着您的程序将不会订阅(并且不会对收到的任何消息执行任何操作)。
除了上面提到的,你同时使用了线程和阻塞循环功能。这会导致问题,因为你最终运行了网络循环的多个示例。我修复了这些问题,并做了一些其他的小改动:

import paho.mqtt.client as mqtt
import time

username='user'
password='passwd'

def on_message(client, userdata, message):
    print("[SUB] received message: %s" % message.payload.decode("utf-8"))

def on_connect(client, userdata, flags, rc):
    print("[SUB] Connected with result code "+str(rc))
    client.subscribe("/Temp")

time.sleep(5)# Allow time for mosquitto to startup

mqtt_broker = "mqtt"

client = mqtt.Client("sub")
client.on_message = on_message
client.on_connect = on_connect
ttl = 120
client.username_pw_set(username, password)
client.connect(mqtt_broker, 1883, ttl)

#client.loop_start() # Pick either threaded or non-threaded, not both!

client.subscribe("Temp")
print("[SUB] Awaiting notifications")
client.loop_forever()

我使用docker运行了您的代码和m蚊- config如下(如果需要,很高兴添加Dockerfile等):

version: '3.4'

services:
  mqtt:
    image: eclipse-mosquitto:latest
    ports:
      - "1883:1883"
    volumes:
      - ./conf:/mosquitto/config
      - ./log:/mosquitto/log
  pythondocker:
    image: pythondocker
    build:
      context: .
      dockerfile: ./Dockerfile

在运行此命令的情况下,我在主机上使用mosquitto_pub.exe -h 127.0.0.1 -u user -P passwd -t Temp -m "foo"发送了一条成功接收的消息。

Attaching to pythondocker-mqtt-1, pythondocker-pythondocker-1
pythondocker-pythondocker-1  | [SUB] Awaiting notifications
pythondocker-pythondocker-1  | [SUB] Connected with result code 0
pythondocker-pythondocker-1  | [SUB] received message: foo

如果我修改python,将密码更改为passwd2,则会出现您看到的错误(不确定您为什么会出现此错误-我使用的是您提供的passwd.txt):

1670453773: New connection from 172.26.0.2:40037 on port 1883.
1670453773: Client sub disconnected, not authorised.

相关问题