Call this method inside your main loop to get the server to check for new incoming client requests. When a request comes in, it will be handled by the handler function. Returns str representing the result of the poll e.g. ``NO_REQUEST`` or ``REQUEST_HANDLED_RESPONSE
(
self,
)
| 461 | response._headers.setdefault(name, value) |
| 462 | |
| 463 | def poll( |
| 464 | self, |
| 465 | ) -> str: |
| 466 | """ |
| 467 | Call this method inside your main loop to get the server to check for new incoming client |
| 468 | requests. When a request comes in, it will be handled by the handler function. |
| 469 | |
| 470 | Returns str representing the result of the poll |
| 471 | e.g. ``NO_REQUEST`` or ``REQUEST_HANDLED_RESPONSE_SENT``. |
| 472 | """ |
| 473 | if self.stopped: |
| 474 | raise ServerStoppedError |
| 475 | |
| 476 | conn = None |
| 477 | try: |
| 478 | if self.debug: |
| 479 | _debug_start_time = monotonic() |
| 480 | |
| 481 | conn, client_address = self._sock.accept() |
| 482 | conn.settimeout(self._timeout) |
| 483 | |
| 484 | # Receive the whole request |
| 485 | if (request := self._receive_request(conn, client_address)) is None: |
| 486 | conn.close() |
| 487 | return CONNECTION_TIMED_OUT |
| 488 | |
| 489 | # Find a route that matches the request's method and path and get its handler |
| 490 | handler = self._find_handler(request.method, request.path) |
| 491 | |
| 492 | # Handle the request |
| 493 | response = self._handle_request(request, handler) |
| 494 | |
| 495 | if response is None: |
| 496 | conn.close() |
| 497 | return REQUEST_HANDLED_NO_RESPONSE |
| 498 | |
| 499 | self._set_default_server_headers(response) |
| 500 | |
| 501 | # Send the response |
| 502 | response._send() |
| 503 | |
| 504 | if self.debug: |
| 505 | _debug_end_time = monotonic() |
| 506 | _debug_response_sent(response, _debug_end_time - _debug_start_time) |
| 507 | |
| 508 | return REQUEST_HANDLED_RESPONSE_SENT |
| 509 | |
| 510 | except Exception as error: |
| 511 | if isinstance(error, OSError): |
| 512 | # There is no data available right now, try again later. |
| 513 | if error.errno == EAGAIN: |
| 514 | return NO_REQUEST |
| 515 | # Connection reset by peer, try again later. |
| 516 | if error.errno == ECONNRESET: |
| 517 | return NO_REQUEST |
| 518 | # Handshake failed, try again later. |
| 519 | if error.errno == MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE: |
| 520 | return NO_REQUEST |
no test coverage detected