5454MQTT_TOPIC_LENGTH_LIMIT = const (65535 )
5555MQTT_TCP_PORT = const (1883 )
5656MQTT_TLS_PORT = const (8883 )
57- TCP_MODE = const (0 )
58- TLS_MODE = const (2 )
5957
6058# MQTT Commands
6159MQTT_PINGREQ = b"\xc0 \0 "
@@ -128,11 +126,7 @@ def __init__(
128126 keep_alive = 60 ,
129127 ):
130128 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
136130 # port/ssl
137131 self .port = MQTT_TCP_PORT
138132 if is_ssl :
@@ -222,18 +216,16 @@ def connect(self, clean_session=True):
222216 :param bool clean_session: Establishes a persistent session.
223217
224218 """
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 ()
229220 self ._sock .settimeout (15 )
230221 if self .port == 8883 :
231222 try :
232223 if self .logger is not None :
233224 self .logger .debug (
234225 "Attempting to establish secure MQTT connection..."
235226 )
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 )
237229 except RuntimeError as e :
238230 raise MMQTTException ("Invalid broker address defined." , e )
239231 else :
@@ -242,7 +234,10 @@ def connect(self, clean_session=True):
242234 self .logger .debug (
243235 "Attempting to establish insecure MQTT connection..."
244236 )
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 )
246241 except RuntimeError as e :
247242 raise MMQTTException ("Invalid broker address defined." , e )
248243
0 commit comments