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,34 +216,16 @@ def connect(self, clean_session=True):
222216 :param bool clean_session: Establishes a persistent session.
223217
224218 """
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 ()
245220 self ._sock .settimeout (15 )
246221 if self .port == 8883 :
247222 try :
248223 if self .logger is not None :
249224 self .logger .debug (
250225 "Attempting to establish secure MQTT connection..."
251226 )
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 )
253229 except RuntimeError as e :
254230 raise MMQTTException ("Invalid broker address defined." , e )
255231 else :
@@ -258,7 +234,10 @@ def connect(self, clean_session=True):
258234 self .logger .debug (
259235 "Attempting to establish insecure MQTT connection..."
260236 )
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 )
262241 except RuntimeError as e :
263242 raise MMQTTException ("Invalid broker address defined." , e )
264243
0 commit comments