Coder Social home page Coder Social logo

libcoapy's Introduction

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())

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.

libcoapy's People

Contributors

anyc avatar

Watchers

Lucian avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.