import paho.mqtt.publish as publish
import paho.mqtt.client as mqtt
USERNAME = "admin-user"
PASSWORD = "admin-user@12345"
AUTH = {"username":USERNAME, "password":PASSWORD}
HOSTNAME = "ssl://b-c7d1ea8g-f777-4c71-23a3-2d73088fdb64-1.mq.us-west-2.amazonaws.com"
PORT = 8883
TOPICNAME = "paho/test/single"
PAYLOAD = "Hope Stackoverflow Help Me Resolve This Issue!"
publish.single(TOPICNAME, payload=PAYLOAD, hostname=HOSTNAME, port=PORT, auth=AUTH, protocol=mqtt.MQTTv311, transport="tcp", client_id="", keepalive=60, will=None) # This won't work
# publish.single(TOPICNAME, payload=PAYLOAD, hostname="localhost") # This works!
print('published message payload = ', PAYLOAD)
我可以使用 Java eclipse Paho 客户端连接到 AmazonMQ,但无法在 Python 中进行相同的操作。它给了我一个错误“ gaierror: [Errno -2] Name or service not known ”。
上面的 python 代码适用于本地托管的 ActiveMQ MQTT 代理,也适用于我托管 ActiveMQ MQTT 代理的独立服务器。但同样不适用于 AmazonMQ 代理。
Github Paho的问题跟踪器中已经提到了这个问题,但仍然没有解决方案可以提供帮助。