54
54
MQTT_TOPIC_LENGTH_LIMIT = const (65535 )
55
55
MQTT_TCP_PORT = const (1883 )
56
56
MQTT_TLS_PORT = const (8883 )
57
- TCP_MODE = const (0 )
58
- TLS_MODE = const (2 )
59
57
60
58
# MQTT Commands
61
59
MQTT_PINGREQ = b"\xc0 \0 "
@@ -128,11 +126,7 @@ def __init__(
128
126
keep_alive = 60 ,
129
127
):
130
128
self ._sock = None
131
- # broker
132
- try : # set broker IP
133
- self .broker = _the_interface .unpretty_ip (broker )
134
- except ValueError : # set broker URL
135
- self .broker = broker
129
+ self .broker = broker
136
130
# port/ssl
137
131
self .port = MQTT_TCP_PORT
138
132
if is_ssl :
@@ -222,34 +216,16 @@ def connect(self, clean_session=True):
222
216
:param bool clean_session: Establishes a persistent session.
223
217
224
218
"""
225
- try :
226
- proto , dummy , broker , path = self .broker .split ("/" , 3 )
227
- # replace spaces in path
228
- path = path .replace (" " , "%20" )
229
- except ValueError :
230
- proto , dummy , broker = self .broker .split ("/" , 2 )
231
- path = ""
232
- if proto == "http:" :
233
- self .port = MQTT_TCP_PORT
234
- elif proto == "https:" :
235
- self .port = MQTT_TLS_PORT
236
- else :
237
- raise ValueError ("Unsupported protocol: " + proto )
238
-
239
- if ":" in broker :
240
- broker , port = broker .split (":" , 1 )
241
- port = int (port )
242
-
243
- addr = _the_sock .getaddrinfo (broker , self .port , 0 , _the_sock .SOCK_STREAM )[0 ]
244
- self ._sock = _the_sock .socket (addr [0 ], addr [1 ], addr [2 ])
219
+ self ._sock = _the_sock .socket ()
245
220
self ._sock .settimeout (15 )
246
221
if self .port == 8883 :
247
222
try :
248
223
if self .logger is not None :
249
224
self .logger .debug (
250
225
"Attempting to establish secure MQTT connection..."
251
226
)
252
- self ._sock .connect ((broker , self .port ), _the_interface .TLS_MODE )
227
+ conntype = _the_interface .TLS_MODE
228
+ self ._sock .connect ((self .broker , self .port ), conntype )
253
229
except RuntimeError as e :
254
230
raise MMQTTException ("Invalid broker address defined." , e )
255
231
else :
@@ -258,7 +234,10 @@ def connect(self, clean_session=True):
258
234
self .logger .debug (
259
235
"Attempting to establish insecure MQTT connection..."
260
236
)
261
- self ._sock .connect (addr [- 1 ], TCP_MODE )
237
+ addr = _the_sock .getaddrinfo (
238
+ self .broker , self .port , 0 , _the_sock .SOCK_STREAM
239
+ )[0 ]
240
+ self ._sock .connect (addr [- 1 ], _the_interface .TCP_MODE )
262
241
except RuntimeError as e :
263
242
raise MMQTTException ("Invalid broker address defined." , e )
264
243
0 commit comments