【发布时间】:2019-03-04 21:53:13
【问题描述】:
我正在尝试从 python 脚本 (paho.mqtt.python) 连接到 mosquitto 代理。 我可以使用以下命令从终端连接:
mosquitto_sub -h localhost -p 8883 -v -t 'owntracks/#' -u owntracks -P 12qwaszx
但是当我尝试通过 python 脚本连接时,我收到了错误:
Socket error on client <unknown>, disconnecting.
我正在使用的脚本是示例: (来自这里:https://owntracks.org/booklet/tech/program/)
import paho.mqtt.client as mqtt
import json
# The callback for when the client successfully connects to the broker
def on_connect(client, userdata, rc):
''' We subscribe on_connect() so that if we lose the connection
and reconnect, subscriptions will be renewed. '''
client.subscribe("owntracks/+/+")
#tried also: client.subscribe("owntracks/#")
# The callback for when a PUBLISH message is received from the broker
def on_message(client, userdata, msg):
topic = msg.topic
try:
data = json.loads(str(msg.payload))
print "TID = {0} is currently at {1}, {2}".format(data['tid'], data['lat'], data['lon'])
except:
print "Cannot decode data on topic {0}".format(topic)
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect("localhost", 8883, 60)
# Blocking call which processes all network traffic and dispatches
# callbacks (see on_*() above). It also handles reconnecting.
client.loop_forever()
这是我的配置文件的内容(我从我的真实 IP 更改了“localhost” - 都尝试了):
# Place your local configuration in /etc/mosquitto/conf.d/
#
# A full description of the configuration file is at
# /usr/share/doc/mosquitto/examples/mosquitto.conf.example
pid_file /var/run/mosquitto.pid
log_dest file /var/log/mosquitto/mosquitto.log
include_dir /etc/mosquitto/conf.d
listener 8883 "localhost"
persistence true
persistence_location /var/lib/mosquitto/
persistence_file mosquitto.db
log_dest syslog
log_dest stdout
log_dest topic
log_type error
log_type warning
log_type notice
log_type information
connection_messages true
log_timestamp true
allow_anonymous false
password_file /etc/mosquitto/pwfile
任何帮助将不胜感激。
【问题讨论】:
-
您使用的是什么版本的 mosquitto,它是如何配置的(具体来说是为 websockets 设置了 8883 端口吗?)
-
版本 1.5.8。它使用此配置进行配置:digitalocean.com/community/questions/…
-
尝试删除它而不是 localhost 或您的 IP 地址(默认情况下,mosquitto 将侦听所有可用的 IP/接口)
-
改了,密码也去掉了,不用指定IP和密码也可以从终端连接,但还是和以前一样的错误。
标签: python python-3.x mqtt mosquitto paho