Skip to content

Commit 1d648df

Browse files
author
brentru
committed
parse out full URLs properly!
1 parent 7f202f3 commit 1d648df

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

adafruit_minimqtt.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,6 @@ def unpretty_ip(ip): # pylint: disable=no-self-use, invalid-name
102102

103103
class MQTT:
104104
"""MQTT Client for CircuitPython
105-
:param socket: Socket object for provided network interface
106105
:param str broker: MQTT Broker URL or IP Address.
107106
:param int port: Optional port definition, defaults to 8883.
108107
:param str username: Username for broker authentication.
@@ -112,6 +111,7 @@ class MQTT:
112111
:param bool is_ssl: Sets a secure or insecure connection with the broker.
113112
:param bool log: Attaches a logger to the MQTT client, defaults to logging level INFO.
114113
:param int keep_alive: KeepAlive interval between the broker and the MiniMQTT client.
114+
115115
"""
116116
# pylint: disable=too-many-arguments,too-many-instance-attributes, not-callable, invalid-name, no-member
117117
def __init__(self, broker, port=None, username=None,
@@ -177,6 +177,7 @@ def __exit__(self, exception_type, exception_value, traceback):
177177
def deinit(self):
178178
"""De-initializes the MQTT client and disconnects from
179179
the mqtt broker.
180+
180181
"""
181182
self.disconnect()
182183

@@ -186,6 +187,7 @@ def last_will(self, topic=None, message=None, qos=0, retain=False):
186187
:param str message: Last will disconnection message.
187188
:param int qos: Quality of Service level.
188189
:param bool retain: Specifies if the message is to be retained when it is published.
190+
189191
"""
190192
if self._is_connected:
191193
raise MMQTTException('Last Will should be defined before connect() is called.')
@@ -202,10 +204,25 @@ def last_will(self, topic=None, message=None, qos=0, retain=False):
202204
def connect(self, clean_session=True):
203205
"""Initiates connection with the MQTT Broker.
204206
:param bool clean_session: Establishes a persistent session.
207+
205208
"""
206-
global _the_interface # pylint: disable=global-statement, invalid-name
207-
global _the_sock # pylint: disable=global-statement, invalid-name
209+
try:
210+
proto, dummy, self.broker, path = self.broker.split("/", 3)
211+
# replace spaces in path
212+
path = path.replace(" ", "%20")
213+
except ValueError:
214+
proto, dummy, self.broker = self.broker.split("/", 2)
215+
path = ""
216+
if proto == "http:":
217+
self.port = MQTT_TCP_PORT
218+
elif proto == "https:":
219+
self.port = MQTT_TLS_PORT
220+
else:
221+
raise ValueError("Unsupported protocol: " + proto)
208222

223+
if ":" in self.broker:
224+
self.broker, port = self.broker.split(":", 1)
225+
port = int(port)
209226

210227
if self.port == 8883:
211228
try:
@@ -218,7 +235,7 @@ def connect(self, clean_session=True):
218235
if isinstance(self.broker, str):
219236
addr = _the_sock.getaddrinfo(self.broker, self.port)[0]
220237
else:
221-
addr = (self.broker, self.port)
238+
addr = (self.broker, 0x21, self.port)
222239
try:
223240
if self.logger is not None:
224241
self.logger.debug('Attempting to establish insecure MQTT connection...')

0 commit comments

Comments
 (0)