on_request Event
Fires when a request is received from a client.
Syntax
class CoAPRequestEventParams(object): @property def remote_host() -> str: ... @property def remote_port() -> int: ... @property def method() -> int: ... @property def uri_host() -> str: ... @property def uri_port() -> int: ... @property def uri_path() -> str: ... @property def uri_query() -> str: ... @property def token() -> bytes: ... @property def request_id() -> str: ... @property def send_response() -> bool: ... @send_response.setter def send_response(value) -> None: ... # In class CoAP: @property def on_request() -> Callable[[CoAPRequestEventParams], None]: ... @on_request.setter def on_request(event_hook: Callable[[CoAPRequestEventParams], None]) -> None: ...
Remarks
This event fires anytime a request is received from a client. Information about the request is exposed both via this event's parameters and the following properties: request_data, request_content_format, request_e_tag, and RequestOption*.
A response can either be sent back to the client immediately when the event finishes, or later using the send_response method. Refer to the RequestId and SendResponse parameters' documentation, below, for more information. If a response is to be sent back immediately, populate the response_code, response_data, response_content_format, response_e_tag, and ResponseOption* properties as desired before this event finishes.
The RemoteHost parameter reflects the client's IP address or hostname.
The RemotePort parameter reflects the client's port.
The Method parameter reflects the request method code. The following table provides a (non-exhaustive) list of some of the more common method codes; refer to the IANA's CoAP Method Codes registry for a full list.
Method Code | Name |
1 | GET |
2 | POST |
3 | PUT |
4 | DELETE |
5 | FETCH |
6 | PATCH |
The URIHost, URIPort, URIPath, and URIQuery parameters, when taken together, identify the resource that the client is making a request against. This event exposes the URI in pieces for convenience.
The Token parameter reflects the token included in the request.
The RequestId parameter reflects the class-generated Id for the request. If the final value of the SendResponse parameter is False, this Id can be passed to the send_response method later to send a response. (Alternatively, it can be passed to the cancel_request later to ignore the request.)
The SendResponse parameter specifies whether the class should send a response back to the client immediately; it is True by default. Set it to False to send the response later instead.
Note: This event is only used when the class is operating in server mode (i.e., when the listening property is enabled).