我对所有这些都很陌生,因此有可能我遗漏了一些明显的东西。我已经搜索了所有内容,但看不到我错了的地方。
我正在尝试使用Python连接到我在Raspberry Pi 3上运行的Mosquitto代理。我使代理运行并使用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。如果我还有其他信息可以帮助您进行故障排除,请告诉我。
答案 0 :(得分:1)
与所有明确正确的行弹出SyntaxError
的情况一样,您的错误出在前一行:
print(msg.payload["name"] + " has finsihed printing in " + int(msg.payload["time"] + "seconds.")
计算括号。