Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 52ea7ca

Browse files
committed
mqtt: Support TLS connections
Activated by specifying mqtts:// scheme instead of mqtt:// By default uses trusted CA certs from system location, but can be overridden with ca_certs MSGFLO_BROKER=mqtts://iot.example.com?ca_certs=/etc/ssl/cert.pem
1 parent 8195772 commit 52ea7ca

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

msgflo/msgflo.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55

66
try:
77
from urllib.parse import urlparse
8+
from urllib.parse import parse_qsl
89
except ImportError:
9-
from urlparse import urlparse
10+
from urlparse import urlparse
11+
from urlparse import parse_qsl
1012

1113
from optparse import OptionParser
1214

@@ -248,6 +250,12 @@ def __init__(self, broker):
248250
self.participants = []
249251
self.connected = False
250252

253+
params = dict(parse_qsl(self.broker_info.query))
254+
default_port = 1883
255+
if self.broker_info.scheme == 'mqtts':
256+
default_port = 8883
257+
ca_certs = params.get('ca_certs')
258+
self._client.tls_set(ca_certs=ca_certs)
251259
if self.broker_info.username:
252260
self._client.username_pw_set(self.broker_info.username, self.broker_info.password)
253261

@@ -257,9 +265,7 @@ def __init__(self, broker):
257265
self._client.on_subscribe = lambda c, u, m, q: self._on_subscribe(c, u, m, q)
258266

259267
host = self.broker_info.hostname
260-
port = self.broker_info.port
261-
if port is None:
262-
port = 1883
268+
port = self.broker_info.port or default_port
263269
self._client.connect(host, port, 60)
264270

265271
def add_participant(self, participant, iips={}):
@@ -387,9 +393,9 @@ def run(participants, broker=None, done_cb=None, iips={}):
387393

388394
engine = None
389395
broker_info = urlparse(broker)
390-
if broker_info.scheme == 'amqp':
396+
if broker_info.scheme in ('amqp', 'amqps'):
391397
engine = AmqpEngine(broker)
392-
elif broker_info.scheme == 'mqtt':
398+
elif broker_info.scheme in ('mqtt', 'mqtts'):
393399
engine = MqttEngine(broker)
394400
else:
395401
raise ValueError("msgflo: No engine implementation found for broker URL scheme %s" % (broker_info.scheme,))

0 commit comments

Comments
 (0)