The usual picture that we have of an Indian snake charmer is of a guy with a flute, with a cobra in a covered basket. When the lid comes off the basket the cobra rears up, spreads its hood, and sways back and forth. It looks like the snake is dancing to the music of the snake charmer's instrument.
But what is really happening is just the natural defensive posture of the cobra. It sways back and forth to get a good look at the flute that is threatening it. Though it can't hear the music, it can feel the vibrations.
Why doesn't it strike ? Probably because it takes a fair amount of provocation to get a cobra to strike at all. Also, the cobra can only strike as far as it can reach from its reared-up position; its charmer can judge this distance and sit a bit further back.
Some street-performing snake charmers extract the fangs of their cobras, or even sew their mouths shut. If the snake dies of starvation, they just get another one.