Skip to content

Commit b9fc2f4

Browse files
committed
match cpython recv timeout behavior. new httpserver example
1 parent 30aa274 commit b9fc2f4

File tree

2 files changed

+51
-7
lines changed

2 files changed

+51
-7
lines changed

adafruit_wiznet5k/adafruit_wiznet5k_socket.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,17 +70,17 @@ def getdefaulttimeout() -> Optional[float]:
7070
return _default_socket_timeout
7171

7272

73-
def setdefaulttimeout(timeout: Optional[float]) -> None:
73+
def setdefaulttimeout(_timeout: Optional[float]) -> None:
7474
"""
7575
Set the default timeout in seconds (float) for new socket objects. When the socket
7676
module is first imported, the default is None. See settimeout() for possible values
7777
and their respective meanings.
7878
79-
:param Optional[float] timeout: The default timeout in seconds or None.
79+
:param Optional[float] _timeout: The default timeout in seconds or None.
8080
"""
8181
global _default_socket_timeout # pylint: disable=global-statement
82-
if timeout is None or (isinstance(timeout, (int, float)) and timeout >= 0):
83-
_default_socket_timeout = timeout
82+
if _timeout is None or (isinstance(_timeout, (int, float)) and _timeout >= 0):
83+
_default_socket_timeout = _timeout
8484
else:
8585
raise ValueError("Timeout must be None, 0.0 or a positive numeric value.")
8686

@@ -450,8 +450,8 @@ def send(self, data: Union[bytes, bytearray]) -> int:
450450
451451
:return int: Number of bytes sent.
452452
"""
453-
timeout = 0 if self._timeout is None else self._timeout
454-
bytes_sent = _the_interface.socket_write(self._socknum, data, timeout)
453+
_timeout = 0 if self._timeout is None else self._timeout
454+
bytes_sent = _the_interface.socket_write(self._socknum, data, _timeout)
455455
gc.collect()
456456
return bytes_sent
457457

@@ -498,7 +498,7 @@ def recv(
498498
stamp = time.monotonic()
499499
while not self._available():
500500
if self._timeout and 0 < self._timeout < time.monotonic() - stamp:
501-
break
501+
raise timeout("timed out")
502502
time.sleep(0.05)
503503
bytes_on_socket = self._available()
504504
if not bytes_on_socket:
@@ -730,3 +730,11 @@ def type(self):
730730
def proto(self):
731731
"""Socket protocol (always 0x00 in this implementation)."""
732732
return 0
733+
734+
735+
class timeout(TimeoutError):
736+
"""TimeoutError class. An instance of this error will be raised by recv_into() if
737+
the timeout has elapsed and we haven't received any data yet."""
738+
739+
def __init__(self, msg):
740+
super().__init__(msg)

examples/wiznet5k_httpserver.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# SPDX-FileCopyrightText: 2023 Tim C for Adafruit Industries
2+
# SPDX-License-Identifier: MIT
3+
4+
import board
5+
import digitalio
6+
from adafruit_httpserver import Server, Request, Response
7+
from adafruit_wiznet5k.adafruit_wiznet5k import WIZNET5K
8+
import adafruit_wiznet5k.adafruit_wiznet5k_socket as socket
9+
10+
print("Wiznet5k HTTPServer Test")
11+
12+
# For Adafruit Ethernet FeatherWing
13+
cs = digitalio.DigitalInOut(board.D10)
14+
# For Particle Ethernet FeatherWing
15+
# cs = digitalio.DigitalInOut(board.D5)
16+
spi_bus = board.SPI()
17+
18+
# Initialize ethernet interface with DHCP
19+
eth = WIZNET5K(spi_bus, cs)
20+
21+
# set the interface on the socket source
22+
socket.set_interface(eth)
23+
24+
# initialize the server
25+
server = Server(socket, "/static", debug=True)
26+
27+
28+
@server.route("/")
29+
def base(request: Request):
30+
"""
31+
Serve a default static plain text message.
32+
"""
33+
return Response(request, "Hello from the CircuitPython HTTP Server!")
34+
35+
36+
server.serve_forever(str(eth.pretty_ip(eth.ip_address)))

0 commit comments

Comments
 (0)