aiocoap.util module

Tools not directly related with CoAP that are needed to provide the API

These are only part of the stable API to the extent they are used by other APIs – for example, you can use the type constructor of ExtensibleEnumMeta when creating an aiocoap.numbers.optionnumbers.OptionNumber, but don’t expect it to be usable in a stable way for own extensions.

Most functions are available in submodules; some of them may only have components that are exclusively used internally and never part of the public API even in the limited fashion stated above.

class aiocoap.util.ExtensibleEnumMeta(name, bases, dict)

Bases: type

Metaclass for ExtensibleIntEnum, see there for detailed explanations

class aiocoap.util.ExtensibleIntEnum

Bases: int

Similar to Python’s enum.IntEnum, this type can be used for named numbers which are not comprehensively known, like CoAP option numbers.

aiocoap.util.hostportjoin(host, port=None)

Join a host and optionally port into a hostinfo-style host:port string

>>> hostportjoin('example.com')
'example.com'
>>> hostportjoin('example.com', 1234)
'example.com:1234'
>>> hostportjoin('127.0.0.1', 1234)
'127.0.0.1:1234'

This is lax with respect to whether host is an IPv6 literal in brackets or not, and accepts either form; IP-future literals that do not contain a colon must be already presented in their bracketed form:

>>> hostportjoin('2001:db8::1')
'[2001:db8::1]'
>>> hostportjoin('2001:db8::1', 1234)
'[2001:db8::1]:1234'
>>> hostportjoin('[2001:db8::1]', 1234)
'[2001:db8::1]:1234'
aiocoap.util.hostportsplit(hostport)

Like urllib.parse.splitport, but return port as int, and as None if not given. Also, it allows giving IPv6 addresses like a netloc:

>>> hostportsplit('foo')
('foo', None)
>>> hostportsplit('foo:5683')
('foo', 5683)
>>> hostportsplit('[::1%eth0]:56830')
('::1%eth0', 56830)
aiocoap.util.quote_nonascii(s)

Like urllib.parse.quote, but explicitly only escaping non-ascii characters.

This function is deprecated due to it use of the irrelevant “being an ASCII character” property (when instead RFC3986 productions like “unreserved” should be used), and due for removal when aiocoap’s URI processing is overhauled the next time.

class aiocoap.util.Sentinel(label)

Bases: object

Class for sentinel that can only be compared for identity. No efforts are taken to make these singletons; it is up to the users to always refer to the same instance, which is typically defined on module level.