Encountering OSError -203 for sockets...no such error code found in errno #16951
-
Hello, I am trying to code an extremely slim webserver with limited feature support. I need a single socket that remains open and serve multiple HTTP requests over that open socket, like a stream. I am encountering a weird issue. Following is my code in a single file main.py.
In this skeleton code I have just tried to bind the socket and wait for a connection. I have not even started consuming the socket data. But I get this error
I can't find what Exception number -203 means. It is a negative number!!! This code is not present in errno module. I can't find this code in google search either. When I comment out the try-exception block 1, I get same OSError -203
line 21 in this line
Funny thing is if I uncomment this line, But why commenting/uncommenting this line is even affecting the 'bind'? My objective is NOT to close the socket once it is opened. But if I don't have the Another useful information is that I hard-reset my ESP32 board between runs by pressing RST button, and this Error comes 80-90% of the total number of runs. I have seen only few times the code does not throw OSError with Appreciate it if anybody can figure out what's wrong in this piece of code. I have a hunch that it has something to do with Socket options. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 5 replies
-
Try: |
Beta Was this translation helpful? Give feedback.
-
I'm not sure if this is 100% correct. To bind a socket you should use socket.getaddrinfo to get the address. On some ports it's a tuple with a str (ip) and an int (port). On Unix it's a bytestring 16 bytes long. from socket import socket, getaddrinfo, AF_INET, SOCK_STREAM, inet_ntop
try:
from socket import AF_INET6
except ImportError:
AF_INET6 = 10
class socket(socket):
"""
Contextmaager for socket
"""
def __enter__(self):
return self
def __exit__(self, exc_type, exc_obj, exc_tb):
self.close()
def create(ip, port, proto):
sock = socket(proto, SOCK_STREAM)
addr = getaddrinfo(ip, port, proto, SOCK_STREAM)[-1][-1]
sock.bind(addr)
return sock
def decode_addr_port(addr):
proto = int.from_bytes(addr[:1])
if proto == 10:
start = 8
else:
start = 4
ip = inet_ntop(proto, addr[start:])
port = int.from_bytes(addr[2:4], "big")
return ip, port
def new_connection(sock, addr):
print(addr)
print(decode_addr_port(addr))
ip = "0.0.0.0"
port = 8885
with create(ip, port, AF_INET) as sock:
sock.listen()
while True:
new_connection(*sock.accept()) |
Beta Was this translation helpful? Give feedback.
-
Strange behavior, since I do not see all of your code, I cannot say what may be the problem. I see that you are using MicroPython v1.23.0. You may want to try a newer version. It is up to you. You are free to write your program any way you want. But, I will make it simpler. Set the server and client at If you like, take a look at my suggestion below: import socket
########## Server Class: BEGIN ############
class SlimServer(object):
def __init__(self, host="0.0.0.0", port=80):
# server
self._server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self._server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self._server.bind((host, port))
self._server.listen(1)
# client - one at a time
self._client = None
def begin(self):
print("SlimServer started")
while True:
try: #try block 2
print("Waiting for new incoming client connection...")
self._client, address = self._server.accept() # this will block !
print("New socket connection arrived...")
print("Client address:", address)
req = {}
req["raw"]= str(self._client.recv(4096), "utf8") #fetch incoming data from socket's rx buffer
#TODO: process the incoming request
print(req["raw"])
except Exception as e: #exception block 2
print("Here 10:")
print(e)
finally: #finally block 2
print("Here 11:")
if self._client:
self._client.close() # close client socket
self._client = None
########## Main: BEGIN ############
server = SlimServer()
server.begin() From another PC (in my case linuxmint) do: $ telnet 192.168.4.73 80
Trying 192.168.4.73...
Connected to 192.168.4.73.
Escape character is '^]'.
12345 I am here
Connection closed by foreign host.
Change the ip to your esp32 ip. You get that from ... or maybe you have a bad esp32 board. |
Beta Was this translation helpful? Give feedback.
You only need to do the bind once. If the
server.bind((host, port))
then your server is not ready to accept any connection.We need to close opened client sockets to reclaim resources.
The example below will keep the client socket open until the client side closes the socket.