Skip to content

Commit

Permalink
Added reuse behaviour for CPython
Browse files Browse the repository at this point in the history
  • Loading branch information
michalpokusa committed Feb 1, 2024
1 parent 8d55322 commit 6944481
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions adafruit_httpserver/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,9 +210,15 @@ def start(self, host: str, port: int = 80) -> None:
self._sock = self._socket_source.socket(
self._socket_source.AF_INET, self._socket_source.SOCK_STREAM
)
self._sock.bind((host, port))
self._sock.listen(10)
self._sock.setblocking(False) # Non-blocking socket
try:
# Only for CPython, prevents "Address already in use" error
self._sock.setsockopt(
self._socket_source.SOL_SOCKET, self._socket_source.SO_REUSEADDR, 1
)
finally:
self._sock.bind((host, port))
self._sock.listen(10)
self._sock.setblocking(False) # Non-blocking socket

if self.debug:
_debug_started_server(self)
Expand Down

0 comments on commit 6944481

Please sign in to comment.