The Python library `awsiot` to pub/sub to AWS IoT used the default port = 443.

Overview

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?

reference: AWS IoT Core can now support MQTT connections with certificate-based client authentication on port 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.

at the end

When using the library, make sure to check how it works. Obviously.

Recommended Posts

The Python library `awsiot` to pub/sub to AWS IoT used the default port = 443.
[Python] How to import the library
How to use the C library in Python
Manage AWS nicely with the Python library Boto
Python mock to try AWS IoT Device Shadow
[AWS / Lambda] How to load Python external library
Feel free to turn Python using the library into an AWS Lambda function
[AWS IoT] Register things in AWS IoT using the AWS IoT Python SDK
I wanted to use the Python library from MATLAB
[Python] How to use the graph creation library Altair
[Introduction to Python] Basic usage of the library matplotlib
How to debug the Python standard library in Visual Studio
[python] How to use the library Matplotlib for drawing graphs
[AWS] Try adding Python library to Layer with SAM + Lambda (Python)
Pass OpenCV data from the original C ++ library to Python
[Python] Frequently used library code
I wrote AWS Lambda, and I was a little addicted to the default value of Python arguments
[Beginner memo] How to specify the library reading path in Python
Leave the troublesome processing to Python
Introduction to Python Numerical Library NumPy
How to use Requests (Python Library)
In the python command python points to python3.8
How to get the Python version
[AWS SAM] Introduction to Python version
[Raspberry Pi] Changed Python default to Python3
[Python] Change the alphabet to numbers
[Introduction to Python] Thorough explanation of the character string type used in Python!