I'm trying to make the following code work after being connected to a wlan:
Code: Select all
def openSocket():
s = socket.socket()
s.bind( ("", 8266) )
s.listen(1)
cliente, a = s.accept()
while True:
try:
data = cliente.recv(1)
except:
print ("Error")
break
print (data[0])
if (data[0] == 49):
led.value(0)
if (data[0] == 50)
led.value(1)
openSocket();
It works as expected, the problem comes when once a client has been connected and disconnects I get:
Traceback (most recent call last):
File "main.py", line 57, in <module>
File "main.py", line 48, in openSocket.
IndexError: Bytes index out of range
when the ESP tries to receive data from the cliente in line data = cliente.recv(1). I understand that the error is caused because there is no socket now that has been closed but I don't know how to handle it to be able to detect that the client has disconnected and then avoid using the recv method.
Can anyone give me a hint in how I could solve this issue?
Thank you.