Skip to content

Commit 5ee757d

Browse files
committed
docs: add examples using cert/key pair
1 parent 9d4615b commit 5ee757d

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed

examples/native_networking/minimqtt_adafruitio_native_networking.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636

3737
### Code ###
3838

39+
3940
# Define callback methods which are called when events occur
4041
# pylint: disable=unused-argument, redefined-outer-name
4142
def connected(client, userdata, flags, rc):
@@ -59,6 +60,14 @@ def message(client, topic, message):
5960

6061
# Create a socket pool
6162
pool = socketpool.SocketPool(wifi.radio)
63+
ssl_context = ssl.create_default_context()
64+
65+
# If you need to use certificate/key pair authentication (e.g. X.509), you can load them in the
66+
# ssl context by uncommenting the line below
67+
# ssl_context.load_cert_chain(
68+
# certfile=secrets['device_cert_path'],
69+
# keyfile=secrets['device_key_path']
70+
# )
6271

6372
# Set up a MiniMQTT Client
6473
mqtt_client = MQTT.MQTT(
@@ -67,7 +76,7 @@ def message(client, topic, message):
6776
username=secrets["aio_username"],
6877
password=secrets["aio_key"],
6978
socket_pool=pool,
70-
ssl_context=ssl.create_default_context(),
79+
ssl_context=ssl_context,
7180
)
7281

7382
# Setup the callback methods above

examples/native_networking/minimqtt_pub_sub_blocking_native_networking.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
# Setup a feed named `testfeed` for publishing.
3333
default_topic = secrets["aio_username"] + "/feeds/testfeed"
3434

35+
3536
### Code ###
3637
# Define callback methods which are called when events occur
3738
# pylint: disable=unused-argument, redefined-outer-name
@@ -59,6 +60,14 @@ def message(client, topic, message):
5960

6061
# Create a socket pool
6162
pool = socketpool.SocketPool(wifi.radio)
63+
ssl_context = ssl.create_default_context()
64+
65+
# If you need to use certificate/key pair authentication (e.g. X.509), you can load them in the
66+
# ssl context by uncommenting the line below
67+
# ssl_context.load_cert_chain(
68+
# certfile=secrets['device_cert_path'],
69+
# keyfile=secrets['device_key_path']
70+
# )
6271

6372
# Set up a MiniMQTT Client
6473
mqtt_client = MQTT.MQTT(
@@ -67,7 +76,7 @@ def message(client, topic, message):
6776
username=secrets["aio_username"],
6877
password=secrets["aio_key"],
6978
socket_pool=pool,
70-
ssl_context=ssl.create_default_context(),
79+
ssl_context=ssl_context,
7180
)
7281

7382
# Setup the callback methods above

examples/native_networking/minimqtt_pub_sub_blocking_topic_callbacks_native_networking.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
### Code ###
3131

32+
3233
# Define callback methods which are called when events occur
3334
# pylint: disable=unused-argument, redefined-outer-name
3435
def connected(client, userdata, flags, rc):
@@ -66,6 +67,14 @@ def on_message(client, topic, message):
6667

6768
# Create a socket pool
6869
pool = socketpool.SocketPool(wifi.radio)
70+
ssl_context = ssl.create_default_context()
71+
72+
# If you need to use certificate/key pair authentication (e.g. X.509), you can load them in the
73+
# ssl context by uncommenting the line below
74+
# ssl_context.load_cert_chain(
75+
# certfile=secrets['device_cert_path'],
76+
# keyfile=secrets['device_key_path']
77+
# )
6978

7079
# Set up a MiniMQTT Client
7180
client = MQTT.MQTT(
@@ -74,7 +83,7 @@ def on_message(client, topic, message):
7483
username=secrets["aio_username"],
7584
password=secrets["aio_key"],
7685
socket_pool=pool,
77-
ssl_context=ssl.create_default_context(),
86+
ssl_context=ssl_context,
7887
)
7988

8089
# Setup the callback methods above

0 commit comments

Comments
 (0)