From b9fc2f4ad1f6022c53b24a9238059127b2223c22 Mon Sep 17 00:00:00 2001 From: foamyguy Date: Mon, 26 Jun 2023 10:38:05 -0500 Subject: [PATCH 1/2] match cpython recv timeout behavior. new httpserver example --- adafruit_wiznet5k/adafruit_wiznet5k_socket.py | 22 ++++++++---- examples/wiznet5k_httpserver.py | 36 +++++++++++++++++++ 2 files changed, 51 insertions(+), 7 deletions(-) create mode 100644 examples/wiznet5k_httpserver.py diff --git a/adafruit_wiznet5k/adafruit_wiznet5k_socket.py b/adafruit_wiznet5k/adafruit_wiznet5k_socket.py index 650fd1f..b788af0 100644 --- a/adafruit_wiznet5k/adafruit_wiznet5k_socket.py +++ b/adafruit_wiznet5k/adafruit_wiznet5k_socket.py @@ -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.") @@ -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 @@ -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: @@ -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) diff --git a/examples/wiznet5k_httpserver.py b/examples/wiznet5k_httpserver.py new file mode 100644 index 0000000..7f7d8b3 --- /dev/null +++ b/examples/wiznet5k_httpserver.py @@ -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))) From 8217dc36ec538b2fd80392103c3373a2cbfbf418 Mon Sep 17 00:00:00 2001 From: foamyguy Date: Mon, 29 Jan 2024 14:48:42 -0600 Subject: [PATCH 2/2] change default mac to string. --- adafruit_wiznet5k/adafruit_wiznet5k.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/adafruit_wiznet5k/adafruit_wiznet5k.py b/adafruit_wiznet5k/adafruit_wiznet5k.py index ddf798a..9b43c76 100644 --- a/adafruit_wiznet5k/adafruit_wiznet5k.py +++ b/adafruit_wiznet5k/adafruit_wiznet5k.py @@ -165,7 +165,7 @@ _MAX_PACKET = const(4000) _LOCAL_PORT = const(0x400) # Default hardware MAC address -_DEFAULT_MAC = (0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED) +_DEFAULT_MAC = "DE:AD:BE:EF:FE:ED" # Maximum number of sockets to support, differs between chip versions. _MAX_SOCK_NUM = {"w5100s": const(0x04), "w5500": const(0x08), "w6100": const(0x08)}