I'm trying to make a very basic server based on the example here: https://docs.micropython.org/en/latest/ ... k_tcp.html
The code below works as expected the first time I run it. But if I run the same program again I get "OSError: 112" on the line with "s.bind(addr)". Changing the port or doing a hard reset solves this.
I think the problem is that I'm trying to bind a port that is already in use. (?)
Any suggestions for how to solve this? Is there a way to unbind all ports?
import socket
import network
#Code for connecting to wifi left out.
html = "<!DOCTYPE html><html><head><title>Test</title></head><body><h1>Hello World!</h1></body></html>"
addr = socket.getaddrinfo('0.0.0.0', 80)[0][-1]
s = socket.socket()
s.bind(addr)
s.listen(1)
print('listening on', addr)
while True:
cl, addr = s.accept()
print('client connected from', addr)
cl_file = cl.makefile('rwb', 0)
while True:
line = cl_file.readline()
if not line or line == b'\r\n':
break
response = html
cl.send(response)
cl.close()
socket problem
Re: socket problem
I am guessing here but maybe try a socket.close() on the socket instance when you are done?
In your case s.close()
Or try garbage collecting manually by using gc.collect(), or both...
In your case s.close()
Or try garbage collecting manually by using gc.collect(), or both...
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: socket problem
If the program is interrupted with ctrl-C sockets will be left open. This can be fixed with judicious use of try ... finally
Most socket problems seem to result from sockets being left open.
On the ESP8266 RAM is limited so I usually issue a soft reset before re-running any program.
Most socket problems seem to result from sockets being left open.
On the ESP8266 RAM is limited so I usually issue a soft reset before re-running any program.
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.
Re: socket problem
Thank you for your replies.
I have added gc.collect() at the start of the program. The problem with this is that if I stop the program and then rerun it, the socket does not appear to be garbage collected. But if I wait a few minutes before running the program again it does work.
s.close() is fine if I'm done with the scoket and can close it, but I have not figured out how to use it if the program is stoped (ctrl-c).
Should doing a soft reset close open ports? It doesn't appear to do so.
Btw, I'm using an ESP32.
I have added gc.collect() at the start of the program. The problem with this is that if I stop the program and then rerun it, the socket does not appear to be garbage collected. But if I wait a few minutes before running the program again it does work.
s.close() is fine if I'm done with the scoket and can close it, but I have not figured out how to use it if the program is stoped (ctrl-c).
Should doing a soft reset close open ports? It doesn't appear to do so.
Btw, I'm using an ESP32.
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: socket problem
Try this. It will "lock up" in a loop until you press ctrl-c, when it will print "Close socket".
Code: Select all
try:
while 1:
pass
except KeyboardInterrupt:
pass # Optionally suppress keyboard interrupt traceback
finally:
print('Close socket')
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.