我对这一切都是新手,所以我可能遗漏了一些明显的东西。我已经找遍了,但我看不出我错在哪里。
我正在尝试使用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。如果有任何其他信息我可以提供,以帮助解决问题,请让我知道。
发布于 2018-06-26 06:27:21
与明确正确的行弹出SyntaxError
的所有情况一样,您的错误出现在前一行:
print(msg.payload["name"] + " has finsihed printing in " + int(msg.payload["time"] + "seconds.")
数一下括号。
https://stackoverflow.com/questions/51032555
复制相似问题