Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

match cpython recv timeout behavior. new httpserver example #125

Merged
merged 3 commits into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions adafruit_wiznet5k/adafruit_wiznet5k_socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,17 @@ def getdefaulttimeout() -> Optional[float]:
return _default_socket_timeout


def setdefaulttimeout(timeout: Optional[float]) -> None:
def setdefaulttimeout(_timeout: Optional[float]) -> None:
"""
Set the default timeout in seconds (float) for new socket objects. When the socket
module is first imported, the default is None. See settimeout() for possible values
and their respective meanings.

:param Optional[float] timeout: The default timeout in seconds or None.
:param Optional[float] _timeout: The default timeout in seconds or None.
"""
global _default_socket_timeout # pylint: disable=global-statement
if timeout is None or (isinstance(timeout, (int, float)) and timeout >= 0):
_default_socket_timeout = timeout
if _timeout is None or (isinstance(_timeout, (int, float)) and _timeout >= 0):
_default_socket_timeout = _timeout
else:
raise ValueError("Timeout must be None, 0.0 or a positive numeric value.")

Expand Down Expand Up @@ -450,8 +450,8 @@ def send(self, data: Union[bytes, bytearray]) -> int:

:return int: Number of bytes sent.
"""
timeout = 0 if self._timeout is None else self._timeout
bytes_sent = _the_interface.socket_write(self._socknum, data, timeout)
_timeout = 0 if self._timeout is None else self._timeout
bytes_sent = _the_interface.socket_write(self._socknum, data, _timeout)
gc.collect()
return bytes_sent

Expand Down Expand Up @@ -498,7 +498,7 @@ def recv(
stamp = time.monotonic()
while not self._available():
if self._timeout and 0 < self._timeout < time.monotonic() - stamp:
break
raise timeout("timed out")
time.sleep(0.05)
bytes_on_socket = self._available()
if not bytes_on_socket:
Expand Down Expand Up @@ -730,3 +730,11 @@ def type(self):
def proto(self):
"""Socket protocol (always 0x00 in this implementation)."""
return 0


class timeout(TimeoutError):
"""TimeoutError class. An instance of this error will be raised by recv_into() if
the timeout has elapsed and we haven't received any data yet."""

def __init__(self, msg):
super().__init__(msg)
36 changes: 36 additions & 0 deletions examples/wiznet5k_httpserver.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# SPDX-FileCopyrightText: 2023 Tim C for Adafruit Industries
# SPDX-License-Identifier: MIT

import board
import digitalio
from adafruit_httpserver import Server, Request, Response
from adafruit_wiznet5k.adafruit_wiznet5k import WIZNET5K
import adafruit_wiznet5k.adafruit_wiznet5k_socket as socket

print("Wiznet5k HTTPServer Test")

# For Adafruit Ethernet FeatherWing
cs = digitalio.DigitalInOut(board.D10)
# For Particle Ethernet FeatherWing
# cs = digitalio.DigitalInOut(board.D5)
spi_bus = board.SPI()

# Initialize ethernet interface with DHCP
eth = WIZNET5K(spi_bus, cs)

# set the interface on the socket source
socket.set_interface(eth)

# initialize the server
server = Server(socket, "/static", debug=True)


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


server.serve_forever(str(eth.pretty_ip(eth.ip_address)))