首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在Python Paho-MQTT中使用SyntaxError = mqtt.Client()时的客户端

在Python Paho-MQTT中使用SyntaxError = mqtt.Client()时的客户端
EN

Stack Overflow用户
提问于 2018-06-26 06:24:41
回答 1查看 784关注 0票数 0

我对这一切都是新手,所以我可能遗漏了一些明显的东西。我已经找遍了,但我看不出我错在哪里。

我正在尝试使用Python连接到我在Raspberry Pi 3上运行的Mosquitto代理。我已经运行了该代理,并使用Mosquitto客户端工具进行了测试。当我尝试运行我的Python脚本时,我得到以下错误:

  File "mqtt_sub.py", line 20
    client = mqtt.Client()
SyntaxError: invalid syntax

下面是我的mqtt_sub.py脚本:

import paho.mqtt.client as mqtt

# The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, flags, rc):
    print("Connected with result code "+str(rc))

    # Subscribing in on_connect() - if we lose the connection and
    # reconnect then subscriptions will be renewed.
    client.subscribe("Kastor/#")
    #client.subscribe("<MainTopic/SubTopic>") # Add additional subscriptions here.

# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
    print(msg.topic+" "+str(msg.payload))

    if msg.topic == "Kastor/event/PrintDone":
        print(msg.payload["name"] + " has finsihed printing in " + int(msg.payload["time"] + "seconds.")

# Create an MQTT client and attach our routines to it.
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message

client.username_pw_set(”myUsername”, ”myPassword”)
client.connect("localhost", 1883, 60)

client.loop_forever()

我在运行脚本的Pi上安装了Python 2.7.9。如果有任何其他信息我可以提供,以帮助解决问题,请让我知道。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-26 06:27:21

与明确正确的行弹出SyntaxError的所有情况一样,您的错误出现在前一行:

print(msg.payload["name"] + " has finsihed printing in " + int(msg.payload["time"] + "seconds.")

数一下括号。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51032555

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档