Skip to content

Commit 4055162

Browse files
committed
Merge branch 'Add_timeout' into my-main
2 parents 22b57fd + 1070165 commit 4055162

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

adafruit_httpserver.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
except ImportError:
2525
pass
2626

27-
from errno import EAGAIN, ECONNRESET
27+
from errno import EAGAIN, ECONNRESET, ETIMEDOUT
2828
import os
2929

3030
__version__ = "0.0.0+auto.0"
@@ -381,17 +381,23 @@ def start(self, host: str, port: int = 80, root: str = "") -> None:
381381
self._sock.listen(10)
382382
self._sock.setblocking(False) # non-blocking socket
383383

384-
def poll(self):
384+
def poll(self, timeout=-1):
385385
"""
386386
Call this method inside your main event loop to get the server to
387387
check for new incoming client requests. When a request comes in,
388388
the application callable will be invoked.
389+
390+
:param int timeout: Optional timeout value in seconds.
389391
"""
390392
try:
391393
conn, _ = self._sock.accept()
392394
with conn:
393395
conn.setblocking(True)
396+
if timeout > 0:
397+
conn.settimeout(timeout)
394398
length, _ = conn.recvfrom_into(self._buffer)
399+
if length <= 0:
400+
return
395401

396402
request = _HTTPRequest(raw_request=self._buffer[:length])
397403

@@ -416,9 +422,12 @@ def poll(self):
416422
if ex.errno == EAGAIN:
417423
# there is no data available right now, try again later.
418424
return
419-
if ex.errno == ECONNRESET:
425+
elif ex.errno == ECONNRESET:
420426
# connection reset by peer, try again later.
421427
return
428+
elif ex.errno == ETIMEDOUT:
429+
# connection timed out
430+
return
422431
raise
423432

424433
@property

0 commit comments

Comments
 (0)