Skip to main content

Python module to communicate over the CoAP protocol

Project description

libcoapy

libcoapy project enables communication over the CoAP protocol (RFC 7252). The llapi module provides ctypes-based wrappers for the libcoap C library. The libcoapy module uses llapi to provide a high-level class interface to the libcoap functions.

Dependencies:

  • libcoap
  • ifaddr (optional, to query all IPs of an interface)
  • netifaces (optional, to query all IPs of an interface)

Status

This project is still in early development. Several functions of the libcoap library are not yet available and existing high-level libcoapy APIs might change in the future.

Portability

libcoapy is a pure python module and the underlying libcoap supports several platforms like Linux, Windows, MacOS and Android. However, libcoap (and hence libcoapy) does not support all features on all platforms and with all possible SSL/TLS libraries.

If you want to use libcoapy with asyncio on platforms without epoll, like Windows, it might be necessary to choose an event loop that supports add_reader(). On Windows you might need to add this to your code before initializing the loop:

if sys.version_info[0] == 3 and sys.version_info[1] >= 8 and sys.platform.startswith('win'):
	asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())

Tools

  • coap-gui - a small Tkinter-based GUI to interact with a CoAP server
  • coarl - a CLI tool that provides a similar interface as curl

Example: client

from libcoapy import *

if len(sys.argv) < 2:
	uri_str = "coap://localhost"
else:
	uri_str = sys.argv[1]

ctx = CoapContext()

session = ctx.newSession(uri_str)

def rx_cb(session, tx_msg, rx_msg, mid):
	print(rx_msg.payload)
	session.ctx.stop_loop()

session.sendMessage(path=".well-known/core", response_callback=rx_cb)

ctx.loop()

Example: server

from libcoapy import *

def echo_handler(resource, session, request, query, response):
	response.payload = request.payload

def time_handler(resource, session, request, query, response):
	import datetime
	now = datetime.datetime.now()
	response.payload = str(now)

ctx = CoapContext()
ctx.addEndpoint("coap://[::]")

time_rs = CoapResource(ctx, "time")
time_rs.addHandler(time_handler)
ctx.addResource(time_rs)

echo_rs = CoapResource(ctx, "echo")
echo_rs.addHandler(echo_handler)
ctx.addResource(echo_rs)

ctx.loop()

More examples can be found in the examples directory.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

libcoapy-2024.7.28.tar.gz (24.4 kB view details)

Uploaded Source

Built Distribution

libcoapy-2024.7.28-py3-none-any.whl (24.6 kB view details)

Uploaded Python 3

File details

Details for the file libcoapy-2024.7.28.tar.gz.

File metadata

  • Download URL: libcoapy-2024.7.28.tar.gz
  • Upload date:
  • Size: 24.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.12.3

File hashes

Hashes for libcoapy-2024.7.28.tar.gz
Algorithm Hash digest
SHA256 5d4cf1ec11e1896283e32684386d31876b35de876f6657d33071f9b992fabf8f
MD5 2234f11057d6541fcb6afab53eeb9a0a
BLAKE2b-256 b28fb74a520c4f52174ee3c204110e116a46d3e658b50c58d2857768d9452085

See more details on using hashes here.

File details

Details for the file libcoapy-2024.7.28-py3-none-any.whl.

File metadata

  • Download URL: libcoapy-2024.7.28-py3-none-any.whl
  • Upload date:
  • Size: 24.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.12.3

File hashes

Hashes for libcoapy-2024.7.28-py3-none-any.whl
Algorithm Hash digest
SHA256 198d17f2f580b82086c2f90b515104b81e77e52a0acfaa8d014ef7460afb042a
MD5 cc52f5e66010064799a9032680b39a41
BLAKE2b-256 20158f4aaf853e201b7e627d0b49844c789eb3f51343d94d2dc1c62eb72eeee0

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page