coapy.option

CoAP defines a variety of options that affect request and response semantics. This module provides classes and functions to operate on native Python instances of the options, and to translate them between the Python instances and their encoded representation within messages

copyright:Copyright 2013, Peter A. Bigot
license:Apache-2.0

Operations

coapy.option.all_options()[source]

Return an iterable producing all registered options.

coapy.option.find_option(number)[source]

Look up an option by number.

Returns the UrOption subclass registered for number, or None if no such option has been registered. number must be an int in the range 0 through 65535.

coapy.option.is_critical_option(number)[source]

Return True iff number identifies a critical option.

A critical option is one that must be understood by the endpoint processing the message. This is indicated by bit 0 (0x01) of the number being set.

coapy.option.is_unsafe_option(number)[source]

Return True iff the option number identifies an unsafe option.

An unsafe option is one that must be recognized by a proxy in order to safely forward (or cache) the message. This is indicated by bit 1 (0x02) of the number being set.

coapy.option.is_no_cache_key_option(number)[source]

Return True iff the option number identifies a NoCacheKey option.

A NoCacheKey option is one for which the value of the option does not contribute to the key that identifies a matching value in a cache. This is encoded in bits 1 through 5 of the number. Options that are unsafe are always NoCacheKey options.

coapy.option.encode_options(options)[source]

Encode a set of options into packed form.

This returns a bytes object that represents the encoding of options after they have been sorted. It may raise an exception if an option’s value cannot be encoded, but performs no semantic validation.

coapy.option.decode_options(data)[source]

Extract a list of options from the packed data which is bytes.

Returns (options, remaining_data) where options is a list of instances of subclasses of UrOption. Options that are unknown to the infrastructure will be returned as instances of UnrecognizedOption. remaining_data will be the suffix of data that was not consumed when unpacking the options.

This will raise OptionDecodeError or other exceptions if the option data is malformed, but does no semantic validation

coapy.option.replace_unacceptable_options(options, is_request)[source]

Verify that a set of options passes CoAP requirements.

Individual options have occurrence restrictions that are encoded within the options themselves and may be inspected by these methods:

CoAP Section 5.4.5 specifies that options violating these constraints are to be interpreted as unrecognized options. These are subsequently used to check for critical or unsafe options.

This method replaces individual recognized options in options with an equivalent unrecognized option instance based on is_request and each option’s restrictions. The resulting list of options is returned.

coapy.option.sorted_options(options)[source]

Sort a sequence of options into canonical order.

Return a list with the same elements as options sorted using the option number as the key. The sort is stable: options with the same number remain in their original order. This operation is used for duplicate detection and to calculate the delta required to encode options.

Option Classes

class coapy.option.UrOption(unpacked_value=None, packed_value=None)[source]

Abstract base for CoAP options.

If unpacked_value is not None, value will be initialized with that value; otherwise if packed_value is not None value will be initialized with the corresponding unpacked value; otherwise value will be None until a valid value is assigned.

_get_value()[source]

Contains the value of the option. This is an instance of format.unpacked_type. Only values that pass the restrictions of format may be assigned to this property. Unacceptable values result in TypeError or OptionLengthError.

classmethod all_match(options)[source]

Return the sub-sequence of options in options that are instances of cls.

Note that the test is specifically for instances of the class; an instance of UnrecognizedOption will not be returned just because the number matches.

classmethod first_match(options)[source]

Return the first option in options that’s an instance of cls.

Returns None if no option in the options is an instance of *cls. Note that the test is specifically for instances of the class; an instance of UnrecognizedOption will not be returned just because the number matches.

format = None

An instance of format_empty, format_opaque, format_uint, or format_string. The instance is used to check that the value is acceptable for the option.

is_critical()[source]

Passes self.number to is_critical_option().

is_no_cache_key()[source]

Passes self.number to is_no_cache_key_option().

is_unsafe()[source]

Passes self.number to is_unsafe_option().

name = None

A human-readable standard short name for the option, suitable for diagnostics. This is a read-only attribute with a Unicode string value.

number = None

The option number.

An unsigned integer in the range 0..65535. This is an IANA-registered value with the following policies (RFC 5226):

Option Range Policy
0..255 IETF Review or IESG Approval
256..2047 Specification Required
2048..64999 Designated Expert
65000..65535 Reserved for experiments

The attribute is read-only. Each subclass of UrOption is registered during its definition. InvalidOptionType will be raised if a previously-registered option exists with the same option number.

packed_value[source]

The value of the option in its packed representation.

valid_in_request()[source]

Return True iff this option may appear at least once in a request message.

valid_in_response()[source]

Return True iff this option may appear at least once in a response message.

valid_multiple_in_request()[source]

Return True iff this option may appear multiple times in a request message.

valid_multiple_in_response()[source]

Return True iff this option may appear multiple times in a response message.

value

Contains the value of the option. This is an instance of format.unpacked_type. Only values that pass the restrictions of format may be assigned to this property. Unacceptable values result in TypeError or OptionLengthError.

class coapy.option.UnrecognizedOption(number, unpacked_value=None, packed_value=None)[source]

Bases: coapy.option.UrOption

Contains an option for which the registered UrOption subclass is not available or may not be used.

number must be provided, must be in the range 0 through 65535. unpacked_value and packed_value operate as in UrOption and accept only bytes objects.

UnrecognizedOption instances are structurally accepted in both request and response messages and instances with the same number may appear multiple times in each. Semantic restrictions may apply based on UrOption.is_critical().

Note

CoAP Section 5.4 specifies situations where an option must be treated as an unrecognized option even if its number matches a recognized option.

_get_number()[source]

The option number associated with this option. This is a read-only attribute.

format = <coapy.option.format_opaque object at 0x3d72650>

Unknown options carry their payload as uninterpreted bytes objects with a maximum length of 1034 octets.

classmethod from_option(opt)[source]

Create an UnrecognizedOption from an existing (recognized) option.

This conversion is necessary per CoAP Section 5.4 in various instances where the recognized option is not accepted. Standard option processing then proceeds with the option left unrecognized.

number

The option number associated with this option. This is a read-only attribute.

Base CoAP Options

The following table shows all options that are defined in base CoAP, along with the format and the maximum length of the packed value (and the minimum length if not zero).

Additional options are defined in protocol extensions.

class coapy.option.UriHost(unpacked_value=None, packed_value=None)[source]

Bases: coapy.option.UrOption

Option encoding the Internet host of a requested resource. See CoAP Section 5.10.1.

class coapy.option.UriPort(unpacked_value=None, packed_value=None)[source]

Bases: coapy.option.UrOption

Option encoding the transport-layer port of a requested resource. See CoAP Section 5.10.1.

class coapy.option.UriPath(unpacked_value=None, packed_value=None)[source]

Bases: coapy.option.UrOption

Option encoding (a segment of) the path of a requested resource. This option may occur multiple times. See CoAP Section 5.10.1.

class coapy.option.UriQuery(unpacked_value=None, packed_value=None)[source]

Bases: coapy.option.UrOption

Option encoding (an element of) the query part of a requested resource. This option may occur multiple times. See CoAP Section 5.10.1.

class coapy.option.ProxyUri(unpacked_value=None, packed_value=None)[source]

Bases: coapy.option.UrOption

Option encoding the URI of a request directed through a forward-proxy. See CoAP Section 5.10.2.

If this option appears in a request, none of UriHost, UriPort, UriPath, or UriQuery may appear.

class coapy.option.ProxyScheme(unpacked_value=None, packed_value=None)[source]

Bases: coapy.option.UrOption

Option encoding the schema for a URI of a request directed through a forward-proxy. In this case the remainder of the URI is constructed from UriHost, UriPort, UriPath, or UriQuery options. See CoAP Section 5.10.2.

class coapy.option.ContentFormat(unpacked_value=None, packed_value=None)[source]

Bases: coapy.option.UrOption

Option encoding the representation format of the message payload. See CoAP Section 5.10.3.

APPLICATION_EXI = 47

CoAP Section 12.3 deriving from Efficient XML Interchange (EXI) Format 1.0.

APPLICATION_JSON = 50

CoAP Section 12.3 deriving from RFC 4627.

See CoAP Section 12.3 deriving from RFC 6690.

APPLICATION_OCTET_STREAM = 42

CoAP Section 12.3 deriving from RFC 2045, RFC 2046.

APPLICATION_XML = 41

CoAP Section 12.3 deriving from RFC 3023.

TEXT_PLAIN = 0

See CoAP Section 12.3 deriving from RFC 2046, RFC 3676, RFC 5147.

media_type_for_content = {0: u'text/plain;charset=utf-8', 40: u'application/link-format', 41: u'application/xml', 42: u'application/octet-stream', 47: u'application/exi', 50: u'application/json'}

A map from integer content format values (e.g. TEXT_PLAIN) to the media type string for that format.

class coapy.option.Accept(unpacked_value=None, packed_value=None)[source]

Bases: coapy.option.UrOption

Option encoding the representation format acceptable to a client. See CoAP Section 5.10.4.

class coapy.option.MaxAge(unpacked_value=None, packed_value=None)[source]

Bases: coapy.option.UrOption

Option encoding the maximum time (in seconds) that a response may be cached before it is outdated. See CoAP Section 5.10.5.

class coapy.option.ETag(unpacked_value=None, packed_value=None)[source]

Bases: coapy.option.UrOption

Option used for a resource-local short-hand for a given representation of a resource. See CoAP Section 5.10.6.

class coapy.option.LocationPath(unpacked_value=None, packed_value=None)[source]

Bases: coapy.option.UrOption

Option encoding (a segment of) the path of a resource identified in a response. This option may occur multiple times. See CoAP Section 5.10.7.

class coapy.option.LocationQuery(unpacked_value=None, packed_value=None)[source]

Bases: coapy.option.UrOption

Option encoding (an element of) the query part of a resource identified in a response. This option may occur multiple times. See CoAP Section 5.10.7.

class coapy.option.IfMatch(unpacked_value=None, packed_value=None)[source]

Bases: coapy.option.UrOption

Option used to make requests conditional on an ETag match. See CoAP Section 5.10.8.1.

class coapy.option.IfNoneMatch(unpacked_value=None, packed_value=None)[source]

Bases: coapy.option.UrOption

Option used to make requests conditional absence of a resource. See CoAP Section 5.10.8.2.

class coapy.option.Size1(unpacked_value=None, packed_value=None)[source]

Bases: coapy.option.UrOption

Option providing size (in bytes) of a resource representation in a request. It may appear in an informational role in a diagnostic response. See CoAP Section 5.10.9.

Option Formatter Classes

When encoded option values are packed in space efficient ways depending on the format of the option value.

class coapy.option._format_base(max_length, min_length)[source]

Abstract base for typed option value formatters.

CoAP options encode values as byte sequences in one of several formats including:

max_length is the maximum length of the packed representation in octets. min_length is the minimum length of the packed representation in octets.

_from_packed(value)[source]

‘Virtual’ method implemented by subclasses to do type-specific unpacking. The subclass implementation may assume value is of type bytes and that the length constraints on the packed representation have been checked. It must return an instance of unpacked_type.

_max_length()[source]

The maximum acceptable length of the packed representation, in octets. This is a read-only property.

_min_length()[source]

The minimum acceptable length of the packed representation, in octets. This is a read-only property.

_to_packed(value)[source]

‘Virtual’ method implemented by subclasses to do type-specific packing. The subclass implementation may assume value is of type unpacked_type and that the length constraints on the packed representation will be checked by to_packed().

_to_text(value)[source]

‘Virtual’ method implemented by subclasses to represent the given value known to be in type unpacked_type. Return the text value as required by to_text().

_unpacked_type()[source]

The Python type used for unpacked values. This is a read-only property.

from_packed(packed)[source]

Convert packed to its unpacked form.

If packed is not an instance of bytes then TypeError will be raised with packed as an argument.

If the length of packed is not between min_length and max_length (both inclusive) then OptionLengthError will be raised with packed as an argument.

Otherwise the value is unpacked and a corresponding instance of unpacked_type is returned.

max_length

The maximum acceptable length of the packed representation, in octets. This is a read-only property.

min_length

The minimum acceptable length of the packed representation, in octets. This is a read-only property.

to_packed(value)[source]

Convert value to packed form.

If value is not an instance of unpacked_type then TypeError will be raised with value as an argument.

The return value is a bytes object with a length between min_length and max_length. If value results in a packed format that is not within these bounds, OptionLengthError will be raised with value as an argument.

to_text(value)[source]

Convert an unpacked_type value to a text (unicode) string holding a user-readable representation of the value.

This function does not validate length constraints on the value.

unpacked_type

The Python type used for unpacked values. This is a read-only property.

class coapy.option.format_empty[source]

Bases: coapy.option._format_base

Support options with no value.

The only acceptable value is a zero-length byte string. This is both the packed and unpacked value.

_UnpackedType

alias of str

class coapy.option.format_opaque(max_length, min_length=0)[source]

Bases: coapy.option._format_base

Support options with opaque values.

Unpacked values are instances of bytes, and packing and unpacking is an identity operation.

_UnpackedType

alias of str

class coapy.option.format_uint(max_length)[source]

Bases: coapy.option._format_base

Supports options with variable-length unsigned integer values. max_length is the maximum number of octets in the packed format. The implicit min_length is always zero.

Unpacked values are instances of int. The packed value is big-endian in a bytes string with all zero-valued leading bytes removed. Thus the packed representation of zero is an empty string.

Per the CoAP specification packed values with leading NUL bytes will decode correctly; however, they are still subject to validation against max_length.

_UnpackedType

alias of int

class coapy.option.format_string(max_length, min_length=0)[source]

Bases: coapy.option._format_base

Supports options with text values.

Unpacked values are Python Unicode (text) strings. Packed values are bytes in Net-Unicode form (RFC 5198), a canonicalized utf-8 encoding. Note that, as usual, the max_length and min_length attributes apply to the packed representation, which for non-ASCII text may be longer than the unpacked representation.

_UnpackedType

alias of unicode

Table Of Contents

Previous topic

coapy.message

Next topic

coapy.endpoint

This Page