Skip to content

Error: Failure to bind on ESP when using HTTPS with adafruit_httpserver #8947

Closed
@michalpokusa

Description

@michalpokusa

CircuitPython version

Adafruit CircuitPython 9.0.0-beta.1 on 2024-02-17; Adafruit Feather ESP32-S2 TFT with ESP32S2
Adafruit CircuitPython 9.0.0-beta.1 on 2024-02-17; Adafruit MatrixPortal S3 with ESP32S3

Code/REPL

import ssl
import socketpool
import wifi

from adafruit_httpserver import Server, Request, Response


class TLSServerSocketPool:
    def __init__(self, pool, ssl_context):
        self._pool = pool
        self._ssl_context = ssl_context

    @property
    def AF_INET(self):
        return self._pool.AF_INET

    @property
    def SOCK_STREAM(self):
        return self._pool.SOCK_STREAM

    def socket(self, *args, **kwargs):
        socket = self._pool.socket(*args, **kwargs)

        # For CircuitPython 9.0.0-beta.1 and later
        socket.setsockopt(self._pool.SOL_SOCKET, self._pool.SO_REUSEADDR, 1)

        return self._ssl_context.wrap_socket(socket, server_side=True)

    def getaddrinfo(self, *args, **kwargs):
        return self._pool.getaddrinfo(*args, **kwargs)


pool = socketpool.SocketPool(wifi.radio)

ssl_context = ssl.create_default_context()
ssl_context.load_verify_locations(cadata="")
ssl_context.load_cert_chain("cert.pem", "key.pem")

tls_pool = TLSServerSocketPool(pool, ssl_context)

server = Server(tls_pool, "/static", debug=True)


@server.route("/")
def base(request: Request):
    """
    Serve a default static plain text message.
    """
    return Response(request, "Hello from the CircuitPython HTTPS Server!")


server.serve_forever(str(wifi.radio.ipv4_address), 8000)

Behavior

Auto-reload is on. Simply save files over USB to run them or enter REPL to disable.
code.py output:
Traceback (most recent call last):
  File "code.py", line 52, in <module>
  File "lib/adafruit_httpserver/server.py", line 186, in serve_forever
  File "lib/adafruit_httpserver/server.py", line 226, in start
ValueError: Error: Failure to bind

Code done running.

Description

No response

Additional information

While working on HTTPS support for adafruit_httpserver I noticed taht the latest version of CP changed something that seems to break code taht has been working until now regarding SSLSockets.

It used to work between e3c4b79e29ca691b972ccaad3576375e50ea08b1 and 9.0.0-beta.1, althought then I was getting MemoryError as described here

This is probably related to:
#8268
#8932

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions