I tried to access AWS IoT with python. When I accessed it with awsiot, I used port = 443.
awsiot
Copy and paste this as it is. Issue MQTT messages from your device to AWS IoT Core using Python (https://aws.amazon.com/jp/premiumsupport/knowledge-center/iot-core-publish-mqtt-messages-python/)
from awscrt import io, mqtt, auth, http
from awsiot import mqtt_connection_builder
import time as t
import json
# Define ENDPOINT, CLIENT_ID, PATH_TO_CERT, PATH_TO_KEY, PATH_TO_ROOT, MESSAGE, TOPIC, and RANGE
ENDPOINT = "xxx-ats.iot.us-east-2.amazonaws.com"
CLIENT_ID = "python"
PATH_TO_CERT = "certificates/yyy-certificate.pem.crt"
PATH_TO_KEY = "certificates/yyy-private.pem.key"
PATH_TO_ROOT = "certificates/AmazonRootCA1.pem"
MESSAGE = "Hello World"
TOPIC = "test/testing"
RANGE = 2
# Spin up resources
event_loop_group = io.EventLoopGroup(1)
host_resolver = io.DefaultHostResolver(event_loop_group)
client_bootstrap = io.ClientBootstrap(event_loop_group, host_resolver)
mqtt_connection = mqtt_connection_builder.mtls_from_path(
endpoint=ENDPOINT,
cert_filepath=PATH_TO_CERT,
pri_key_filepath=PATH_TO_KEY,
client_bootstrap=client_bootstrap,
ca_filepath=PATH_TO_ROOT,
client_id=CLIENT_ID,
clean_session=False,
keep_alive_secs=6
)
print("Connecting to {} with client ID '{}'...".format(
ENDPOINT, CLIENT_ID))
# Make the connect() call
connect_future = mqtt_connection.connect()
# Future.result() waits until a result is available
connect_future.result()
print("Connected!")
# Publish message to server desired number of times.
print('Begin Publish')
for i in range (RANGE):
data = "{} [{}]".format(MESSAGE, i+1)
message = {"message" : data}
mqtt_connection.publish(topic=TOPIC, payload=json.dumps(message), qos=mqtt.QoS.AT_LEAST_ONCE)
print("Published: '" + json.dumps(message) + "' to the topic: " + "'test/testing'")
t.sleep(0.1)
print('Publish End')
disconnect_future = mqtt_connection.disconnect()
disconnect_future.result()
When I see the packet with Wireshark, I'm using port 443. seriously. Is it the default 443?
When I specified the port as a trial, it became 8883
.
mqtt_connection = mqtt_connection_builder.mtls_from_path(
endpoint=ENDPOINT,
port=8883,
cert_filepath=PATH_TO_CERT,
pri_key_filepath=PATH_TO_KEY,
client_bootstrap=client_bootstrap,
ca_filepath=PATH_TO_ROOT,
client_id=CLIENT_ID,
clean_session=False,
keep_alive_secs=6
)
paha I also tried the paho code for comparison. I copied and pasted this person's code. Paho(MQTT Client Library) -Python- - Qiita
#!/usr/bin/python
# -*- coding: utf-8 -*-
import paho.mqtt.client as mqtt
import ssl
host = 'xxx-ats.iot.us-east-2.amazonaws.com'
###Used when using password authentication
#username = 'mqtt'
#password = 'mqtt'
### SSL
port = 8883
cacert = './certificates/AmazonRootCA1.pem'
clientCert = './certificates/yyy-certificate.pem.crt'
clientKey = './certificates/yyy-private.pem.key'
topic = "esp32/pub"
message = 'test message'
def on_connect(client, userdata, flags, respons_code):
"""callback function when connecting to broker
"""
print('status {0}'.format(respons_code))
client.publish(topic, message)
def on_publish(client, userdata, mid):
"""Callback function after publishing a message
"""
client.disconnect()
if __name__ == '__main__':
###Protocol v3 when creating an instance.1.Specify 1
client = mqtt.Client(client_id= "python",protocol=mqtt.MQTTv311)
###Used when using password authentication
#client.username_pw_set(username, password=password)
### SSL
client.tls_set(cacert,
certfile = clientCert,
keyfile = clientKey,
tls_version = ssl.PROTOCOL_TLSv1_2)
client.tls_insecure_set(True)
### callback function
client.on_connect = on_connect
client.on_publish = on_publish
client.connect(host, port=port, keepalive=6)
client.loop_forever()
It's natural because I specified it, but I connected with port = 8883.
When using the library, make sure to check how it works. Obviously.
Recommended Posts