← index #1056Issue #2576
Likely Duplicate · high · value 1.976
QUERY · ISSUE

IndexError: bytes index out of range

openby MikeZhou-UofTopened 2025-11-10updated 2025-11-10

When I run the code simple.py on a Pi Pico.

I was getting "IndexError: bytes index out of range" after the internet was connected, but the MQTT broker connection failed.

From line 112 of simple.py: assert resp[0] == 0x20 and resp[1] == 0x02

It was saying "in connect" but afterward, it is giving me IndexError: bytes index out of range. Has anyone had this issue before?

What I have to do is unplug the USB and plug it back in multiple times to make it work.

1 comment
Josverl · 2025-11-10

Likely, in that case, the resp object is shorter than the two bytes.

The library is simple, it does not handle all possible error, so you may need to wrap it in additional error handling,
also to deal with external glitches in networks , DNS and other interferences.

CANDIDATE · ISSUE

MQTT: c.connect() -> IndexError: bytes index out of range

closedby BigMan200opened 2016-10-30updated 2024-08-25

On a raspian (IP=10.0.0.32) I installed mosquitto:
service mosquitto status
gives me: [ ok ] mosquitto is running.

An ESP8266 is in the same network:
FW: esp8266-20161017-v1.8.5.bin
'>>> sta_if.ifconfig()
gives me: ('10.0.0.105', ...)

On the raspian-terminal I can also ping my ESP8266:
ping 10.0.0.105
gives me: 64 bytes from 10.0.0.105: icmp_req=1 ttl=255 time=2.62 ms ...

But I am failing to connect with the MQTT:
'>>> from umqtt.simple import MQTTClient
'>>> c = MQTTClient("client", "10.0.0.32")
'>>> c.connect()
gives me:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "umqtt/simple.py", line 84, in connect
IndexError: bytes index out of range

Keyboard

j / / n
next pair
k / / p
previous pair
1 / / h
show query pane
2 / / l
show candidate pane
c
copy suggested comment
r
toggle reasoning
g i
go to index
?
show this help
esc
close overlays

press ? or esc to close

copied