@@ -201,7 +201,8 @@ def reconnect(self, retries=30, resub_topics=True):
201
201
feed = self ._subscribed_topics .pop ()
202
202
self .subscribe (feed )
203
203
except OSError as e :
204
- print ('Failed to connect to the broker, retrying\n ' , e )
204
+ if self ._logger is not None :
205
+ self ._logger .debug ('Lost connection, reconnecting and resubscribing...' , e )
205
206
retries += 1
206
207
if retries >= 30 :
207
208
retries = 0
@@ -279,8 +280,8 @@ def connect(self, clean_session=True):
279
280
op = self ._wait_for_msg ()
280
281
if op == 32 :
281
282
rc = self ._sock .read (3 )
282
- assert rc [0 ] == const ( 0x02 )
283
- if rc [2 ] != const ( 0x00 ) :
283
+ assert rc [0 ] == 0x02
284
+ if rc [2 ] != 0x00 :
284
285
raise MMQTTException (CONNACK_ERRORS [rc [3 ]])
285
286
self ._is_connected = True
286
287
result = rc [0 ] & 1
@@ -313,11 +314,11 @@ def ping(self):
313
314
self ._sock .write (MQTT_PINGREQ )
314
315
if self ._logger is not None :
315
316
self ._logger .debug ('Checking PINGRESP' )
316
- while 1 :
317
+ while True :
317
318
op = self ._wait_for_msg (0.5 )
318
- if op == const ( 208 ) :
319
+ if op == 208 :
319
320
ping_resp = self ._sock .read (2 )
320
- if ping_resp [0 ] != const ( 0x00 ) :
321
+ if ping_resp [0 ] != 0x00 :
321
322
raise MMQTTException ('PINGRESP not returned from broker.' )
322
323
return
323
324
@@ -368,10 +369,10 @@ def publish(self, topic, msg, retain=False, qos=0):
368
369
sz = 2 + len (topic ) + len (msg )
369
370
if qos > 0 :
370
371
sz += 2
371
- assert sz < const ( 2097152 )
372
+ assert sz < 2097152
372
373
i = 1
373
- while sz > const ( 0x7f ) :
374
- pkt [i ] = (sz & 0x7f ) | const ( 0x80 )
374
+ while sz > 0x7f :
375
+ pkt [i ] = (sz & 0x7f ) | 0x80
375
376
sz >>= 7
376
377
i += 1
377
378
pkt [i ] = sz
@@ -394,13 +395,13 @@ def publish(self, topic, msg, retain=False, qos=0):
394
395
self ._logger .debug ('Sending PUBACK' )
395
396
self ._sock .write (msg )
396
397
if qos == 1 :
397
- while 1 :
398
+ while True :
398
399
op = self ._wait_for_msg ()
399
- if op == const ( 0x40 ) :
400
+ if op == 0x40 :
400
401
sz = self ._sock .read (1 )
401
402
assert sz == b"\x02 "
402
403
rcv_pid = self ._sock .read (2 )
403
- rcv_pid = rcv_pid [0 ] << const ( 0x08 ) | rcv_pid [1 ]
404
+ rcv_pid = rcv_pid [0 ] << 0x08 | rcv_pid [1 ]
404
405
if pid == rcv_pid :
405
406
if self .on_publish is not None :
406
407
self .on_publish (self , self ._user_data , topic , rcv_pid )
@@ -472,7 +473,7 @@ def subscribe(self, topic, qos=0):
472
473
for t , q in topics :
473
474
self ._logger .debug ('SUBSCRIBING to topic {0} with QoS {1}' .format (t , q ))
474
475
self ._sock .write (packet )
475
- while 1 :
476
+ while True :
476
477
op = self ._wait_for_msg ()
477
478
if op == 0x90 :
478
479
rc = self ._sock .read (4 )
@@ -529,11 +530,11 @@ def unsubscribe(self, topic):
529
530
self ._sock .write (packet )
530
531
if self ._logger is not None :
531
532
self ._logger .debug ('Waiting for UNSUBACK...' )
532
- while 1 :
533
+ while True :
533
534
op = self ._wait_for_msg ()
534
- if op == const ( 176 ) :
535
+ if op == 176 :
535
536
return_code = self ._sock .read (3 )
536
- assert return_code [0 ] == const ( 0x02 )
537
+ assert return_code [0 ] == 0x02
537
538
# [MQTT-3.32]
538
539
assert return_code [1 ] == packet_id_bytes [0 ] and return_code [2 ] == packet_id_bytes [1 ]
539
540
for t in topics :
@@ -580,22 +581,22 @@ def _wait_for_msg(self, timeout=30):
580
581
sz = self ._sock .read (1 )[0 ]
581
582
assert sz == 0
582
583
return None
583
- if res [0 ] & const ( 0xf0 ) != const ( 0x30 ) :
584
+ if res [0 ] & 0xf0 != 0x30 :
584
585
return res [0 ]
585
586
sz = self ._recv_len ()
586
587
topic_len = self ._sock .read (2 )
587
588
topic_len = (topic_len [0 ] << 8 ) | topic_len [1 ]
588
589
topic = self ._sock .read (topic_len )
589
590
topic = str (topic , 'utf-8' )
590
591
sz -= topic_len + 2
591
- if res [0 ] & const ( 0x06 ) :
592
+ if res [0 ] & 0x06 :
592
593
pid = self ._sock .read (2 )
593
- pid = pid [0 ] << const ( 0x08 ) | pid [1 ]
594
- sz -= const ( 0x02 )
594
+ pid = pid [0 ] << 0x08 | pid [1 ]
595
+ sz -= 0x02
595
596
msg = self ._sock .read (sz )
596
597
if self .on_message is not None :
597
598
self .on_message (self , topic , str (msg , 'utf-8' ))
598
- if res [0 ] & const ( 0x06 ) == const ( 0x02 ) :
599
+ if res [0 ] & 0x06 == 0x02 :
599
600
pkt = bytearray (b"\x40 \x02 \0 \0 " )
600
601
struct .pack_into ("!H" , pkt , 2 , pid )
601
602
self ._sock .write (pkt )
@@ -606,7 +607,7 @@ def _wait_for_msg(self, timeout=30):
606
607
def _recv_len (self ):
607
608
n = 0
608
609
sh = 0
609
- while 1 :
610
+ while True :
610
611
b = self ._sock .read (1 )[0 ]
611
612
n |= (b & 0x7f ) << sh
612
613
if not b & 0x80 :
0 commit comments