Connection-Related Classes

class coapy.connection.EndPoint(address_family=2, socket_type=2, socket_proto=17)

Bases: object


Process network activity related to CoAP messages on this end-point.

This method is responsible for transmitting (and re-transmitting) outgoing messages, and for receiving incoming messages. It will block until the timeout is reached, or an incoming message is received.

Incoming messages that are transaction responses are, if possible, associated with the transmission record to which they pertain. A ReceptionRecord is returned. The infrastructure does not automatically acknowledge any incoming message; this is an application responsibility.

Parameter:timeout_ms – The maximum time, in milliseconds, that this method should block. A value of None indicates no limit: the method will block forever if no activity occurs.
Returns:rx_record or None
Return type:ReceptionRecord

Register the given socket as a potential source of incoming messages.

The primary socket is automatically registered. This method can be used to associate additional sockets, such as multicast ones, with this end-point.

Note:Regardless of the socket on which a message is received, any response will be transmitted from socket.
send(message, remote)

Transmit a message to the remote.

The message should have a code of either Message.CON or Message.NON; acknowledgements and resets should be generated through the ReceptionRecord methods.

The TransmissionRecord associated with the transmission is returned.

Note:Invoking this does not actually transmit the message: it merely records it and queues it for transmission on the next invocation of process().

Return a reference to the primary socket associated with the end-point.

If this end-point is expected to receive REST requests, the socket should be bound to a known port so that clients can identify the server.

class coapy.connection.Message(transaction_type=0, code=0, payload='', **kw)

Bases: object

Represent the components of a CoAP message.

The transaction ID is not recorded in the message instance. Rather, it is recorded in a TransmissionRecord or ReceptionRecord.

Create a Message instance.

As a convenience, message options can be created from keyword parameters if the keywords are present in OptionKeywords.

  • transaction_type – One of CON, NON, ACK, RST. The message transaction type cannot be modified after creation.
  • code – The integral code identifying the method of a request message, or the disposition in a response message.
  • payload – An optional payload for the message. If not provided, the message will have no payload unless subsequently assigned.
The message transaction type indicating acknowledgement of a confirmable message. Note that such a message may also include a response payload that pertains to the acknowledged message.
The message transaction type indicating a confirmable message (one that requires an acknowledgement).
The message transaction type indicating a non-confirmable message (one that does not evoke an acknowledgement).

A map from Python identifiers to option classes.

These identifiers can be provided as keyword parameters to the Message.__init__() method; the corresponding option class will be invoked with the parameter value to create an option that is associated with the message.

The message transaction type indicating that a received confirmable message could not be processed due to insufficient context.

Add a new option instance.

If the option can appear multiple times, this method is intended to add the new value to the existing ones.

Warning:Currently multi-valued options are not supported and this is equivalent to replaceOption().
The integral request method code or response code of the message.
classmethod decode(packed)

Create a Message instance from a payload.

This method decodes the payload, and returns a pair (xid, message) where xid is the transaction ID extracted from the encoded message, and message is an instance of Message initialized to the decoded components of the data.

Parameter:payload – A sequence of octets comprising a complete CoAP packet
Return type:(int, Message)

Remove the option from the message.

Parameter:opt – An option, specified as an option instance, an option class, or the type code of an option.

Locate the given option within the message.

Returns None if no matching option can be found.

Parameter:opt – An option, specified as an option instance, an option class, or the type code of an option.

A tuple containing the options associated with the message.

The options are sorted in increasing value of option type.


The payload of the message as a str.

If this is not an empty string, there should be a corresponding coapy.options.ContentType option present that defines its format (if the default value of text/plain is not appropriate).


Add a new option instance.

If the option is already present in message, its previous value is replaced by the new one.


Return the transaction type (one of CON, NON, ACK, RST).

Note:The transaction type is assigned when the message is created, and cannot be changed thereafter.
The CoAP protocol version.
class coapy.connection.ReceptionRecord(end_point, packed, remote)

Bases: object

Material related to a received CoAP message.

The EndPoint that received the message.
The Message received.

The TransmissionRecord to which the received message was interpreted as a response.

The value will be None if the message transaction type is neither Message.ACK nor Message.RST, or if the receiving EndPoint could not identify the relevant message (e.g., it had already been expired from the transmission cache).

The socket address from which the message was received.
The transaction ID of the received message.
class coapy.connection.TransmissionRecord(end_point, message, remote)

Bases: object

Material related to a transmitted CoAP message.

  • end_point – The EndPoint responsible for transmitting the message.
  • message – An instance of Message from which the transmission content will be calculated
  • remote – A Python socket address identifying the destination of the transmission.
The EndPoint that transmitted the message.
Return True iff this was a confirmable transaction for which the last transmission response wait has elapsed without receipt of a response.

The time.time() at which the last event related to the transmission occured.

Transmission events are:

  • transmission or retranmission of the message
  • receipt of a response to the message

A reference to the Message from which the transmission derived.

Note:The content of the message may have been changed by application code subsequent to its transmission.

Get the time.time() value at which the next event associated with this transmission is due.

Predictable transmission events are:

  • the time at which the message should be retransmitted
  • the time by which a response to the message should be received

Returns None if there are no events associated with the transmission.

The octet sequence representing the message.
The Python socket address to which the transmission was sent.
The ReceptionRecord for the first message that was interpreted as a response to this message.
  • Message.NON if the message does not require a response
  • Message.ACK if the message has been acknowledged. If the acknowledgement carried a response message, it is available in response.
  • Message.RST if the message received could not process the message.
  • None if the message is confirmable and neither an Message.ACK nor Message.RST has been received.
A set containing all ReceptionRecords that pertain to this transmission.
The transmission ID encoded in the packed message.
The time.time() at which the message was first transmitted.

Return the number of (re-)transmissions yet to occur.

A positive value requires that next_event_time not be None.


Return True iff address is a multicast address.

This function is used to eliminate retransmissions for confirmable messages sent to multicast addresses.

Parameter:address – A socket address as supported by the Python socket functions: i.e. a pair (host, port) for IPv4 addresses and a tuple (host, port, flowinfo, scopeid) for IPv6. The host may be either a host name or an IP address in the textual notation appropriate to the address family.

Previous topic

Link Format

This Page