【问题标题】:socket error when connecting paho.mqtt.python to mosquitto broker将 paho.mqtt.python 连接到 mosquitto 代理时出现套接字错误
【发布时间】: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


【解决方案1】:

您的 python 脚本正在尝试连接到看起来像 TLS 安全设置的东西,而没有准备客户端连接方法来将这些详细信息应用于事务。请尝试以下操作:

def ssl_prep():
        ssl_context = ssl.create_default_context()
        ssl_context.load_verify_locations(cafile=ca)
        ssl_context.load_cert_chain(certfile=mycert, keyfile=priv)
        return  ssl_context

ca = "PATH_TO_YOUR_CA_FILE"
priv = "PATH_TO_YOUR_PEM_FILE"
mycert = "PATH_TO_YOUR_CERT_FILE"
topics = "YOUR_TOPICS"
broker = "BROKER_URL"
client = mqtt.Client()
ssl_context= ssl_prep()

client.tls_set_context(context=ssl_context)
client.username_pw_set(username="UNAME",password="PASS")
client.connect(broker, port=8883)

通过在尝试之前为连接尝试提供 ssl 上下文,这应该会连接,前提是您已准备好特定于您自己的设置的所有详细信息。

【讨论】:

    【解决方案2】:

    尝试将 KeepAlive 时间从 1 分钟减少到 30 秒或更低:

    client.connect("localhost", 8883, 30)
    //Default: connect(host, port=1883, keepalive=60, bind_address=””)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多