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)
I am able to connect to AmazonMQ with Java eclipse Paho client, but not able to do same the in Python. It gives me an error "gaierror: [Errno -2] Name or service not known".
The above python code works fine with a locally hosted ActiveMQ MQTT broker and also in stand alone server where I have hosted ActiveMQ MQTT broker. But the same is not working with AmazonMQ broker.
This issue is already mentioned in the issue tracker of Github Paho, but still there is no solution that will help.
You need to remove the "ssl://" from your host variable, and set a ssl context in order to connect to Amazon-MQ with paho.
Here is a working version of the example posted on your github issue.