【求助】python写的mqtt client,连接apollo总是报错 Error 10054

发布于 2022-09-07 04:03:33 字数 850 浏览 24 评论 0

首先,apollo正常启动

clipboard.png

python代码,很简单:

import time
import paho.mqtt.client as mqtt



def on_connect(client, userdata, flags, rc):
    print("Connected with result code " + str(rc))

if __name__ == "__main__":
    client = mqtt.Client()
    client.username_pw_set(username="admins",password="password")
    client.on_connect = on_connect

    client.connect("127.0.0.1", 61613, 60)
    client.loop_start()


    #while True:
    time.sleep(1)
    client.publish(topic="message", payload="hello")

python2.7编译器,运行后报错

Connected to pydev debugger (build 173.4674.37)
[Errno 10054] 
Exception in thread Thread-6 (most likely raised during interpreter shutdown):
Process finished with exit code 0

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一人独醉 2022-09-14 04:03:33

python代码需要增加一行

import time
import paho.mqtt.client as mqtt



def on_connect(client, userdata, flags, rc):
    print("Connected with result code " + str(rc))

if __name__ == "__main__":
    #client_id是必须的,并且是唯一的。否则可能会出现如下错误
    client_id = time.strftime('%Y%m%d%H%M%S', time.localtime(time.time()))
    client = mqtt.Client(client_id) #ClientId不能重复,所以使用当前时间
    
    client.username_pw_set(username="admins",password="password")
    client.on_connect = on_connect

    client.connect("127.0.0.1", 61613, 60)
    client.loop_start()


    #while True:
    time.sleep(1)
    client.publish(topic="message", payload="hello")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文