Skip to content

Commit e3c83b4

Browse files
committed
Added returns to Server.pool()
1 parent b271a3c commit e3c83b4

File tree

4 files changed

+36
-8
lines changed

4 files changed

+36
-8
lines changed

adafruit_httpserver/__init__.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,13 @@
5959
JSONResponse,
6060
Redirect,
6161
)
62-
from .server import Server
62+
from .server import (
63+
Server,
64+
NO_REQUEST,
65+
CONNECTION_TIMED_OUT,
66+
REQUEST_HANDLED_NO_RESPONSE,
67+
REQUEST_HANDLED_RESPONSE_SENT,
68+
)
6369
from .status import (
6470
Status,
6571
OK_200,

adafruit_httpserver/server.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@
3333
from .status import BAD_REQUEST_400, UNAUTHORIZED_401, FORBIDDEN_403, NOT_FOUND_404
3434

3535

36+
NO_REQUEST = 0
37+
CONNECTION_TIMED_OUT = 1
38+
REQUEST_HANDLED_NO_RESPONSE = 2
39+
REQUEST_HANDLED_RESPONSE_SENT = 3
40+
41+
3642
class Server: # pylint: disable=too-many-instance-attributes
3743
"""A basic socket-based HTTP server."""
3844

@@ -318,10 +324,13 @@ def _set_default_server_headers(self, response: Response) -> None:
318324
name, value
319325
)
320326

321-
def poll(self):
327+
def poll(self) -> int:
322328
"""
323329
Call this method inside your main loop to get the server to check for new incoming client
324330
requests. When a request comes in, it will be handled by the handler function.
331+
332+
Returns int representing the result of the poll
333+
e.g. ``NO_REQUEST`` or ``REQUEST_HANDLED_RESPONSE_SENT``.
325334
"""
326335
if self.stopped:
327336
raise ServerStoppedError
@@ -333,7 +342,7 @@ def poll(self):
333342

334343
# Receive the whole request
335344
if (request := self._receive_request(conn, client_address)) is None:
336-
return
345+
return CONNECTION_TIMED_OUT
337346

338347
# Find a handler for the route
339348
handler = self._routes.find_handler(
@@ -344,7 +353,7 @@ def poll(self):
344353
response = self._handle_request(request, handler)
345354

346355
if response is None:
347-
return
356+
return REQUEST_HANDLED_NO_RESPONSE
348357

349358
self._set_default_server_headers(response)
350359

@@ -354,14 +363,16 @@ def poll(self):
354363
if self.debug:
355364
_debug_response_sent(response)
356365

366+
return REQUEST_HANDLED_RESPONSE_SENT
367+
357368
except Exception as error: # pylint: disable=broad-except
358369
if isinstance(error, OSError):
359370
# There is no data available right now, try again later.
360371
if error.errno == EAGAIN:
361-
return
372+
return NO_REQUEST
362373
# Connection reset by peer, try again later.
363374
if error.errno == ECONNRESET:
364-
return
375+
return NO_REQUEST
365376

366377
if self.debug:
367378
_debug_exception_in_handler(error)

docs/examples.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ Between calling ``.poll()`` you can do something useful,
7272
for example read a sensor and capture an average or
7373
a running total of the last 10 samples.
7474

75+
``.poll()`` return value can be used to check if there was a request and if it was handled.
76+
7577
.. literalinclude:: ../examples/httpserver_start_and_poll.py
7678
:caption: examples/httpserver_start_and_poll.py
7779
:emphasize-lines: 24,33

examples/httpserver_start_and_poll.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@
55
import socketpool
66
import wifi
77

8-
from adafruit_httpserver import Server, Request, FileResponse
8+
from adafruit_httpserver import (
9+
Server,
10+
REQUEST_HANDLED_RESPONSE_SENT,
11+
Request,
12+
FileResponse,
13+
)
914

1015

1116
pool = socketpool.SocketPool(wifi.radio)
@@ -30,7 +35,11 @@ def base(request: Request):
3035
# or a running total of the last 10 samples
3136

3237
# Process any waiting requests
33-
server.poll()
38+
pool_result = server.poll()
39+
40+
if pool_result == REQUEST_HANDLED_RESPONSE_SENT:
41+
# Do something only after handling a request
42+
pass
3443

3544
# If you want you can stop the server by calling server.stop() anywhere in your code
3645
except OSError as error:

0 commit comments

Comments
 (0)