在 Python Paho-MQTT 中使用 client = mqtt.Client() 时出现语法错误

SyntaxError when using client = mqtt.Client() in Python Paho-MQTT

我对这一切都很陌生,所以我可能遗漏了一些明显的东西。我到处搜索,但我没有看到我弄错了什么。

我正在尝试使用 Python 连接到我在 Raspberry Pi 上有 运行ning 的 Mosquitto 代理 3. 我有代理 运行ning 和使用 Mosquitto-Client 工具进行测试。当我尝试 运行 我的 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 运行。如果我可以提供任何其他信息来帮助解决问题,请告诉我。

就像所有明确正确的行弹出 SyntaxError 的情况一样,您的错误在上一行:

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

计算括号。