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,18 +216,16 @@ def connect(self, clean_session=True):
222
216
:param bool clean_session: Establishes a persistent session.
223
217
224
218
"""
225
- addr = _the_sock .getaddrinfo (self .broker , self .port , 0 , _the_sock .SOCK_STREAM )[
226
- 0
227
- ]
228
- self ._sock = _the_sock .socket (addr [0 ], addr [1 ], addr [2 ])
219
+ self ._sock = _the_sock .socket ()
229
220
self ._sock .settimeout (15 )
230
221
if self .port == 8883 :
231
222
try :
232
223
if self .logger is not None :
233
224
self .logger .debug (
234
225
"Attempting to establish secure MQTT connection..."
235
226
)
236
- self ._sock .connect ((self .broker , self .port ), _the_interface .TLS_MODE )
227
+ conntype = _the_interface .TLS_MODE
228
+ self ._sock .connect ((self .broker , self .port ), conntype )
237
229
except RuntimeError as e :
238
230
raise MMQTTException ("Invalid broker address defined." , e )
239
231
else :
@@ -242,7 +234,10 @@ def connect(self, clean_session=True):
242
234
self .logger .debug (
243
235
"Attempting to establish insecure MQTT connection..."
244
236
)
245
- 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 )
246
241
except RuntimeError as e :
247
242
raise MMQTTException ("Invalid broker address defined." , e )
248
243
0 commit comments