From dd7dbd113650289ddc2f3328854179dab88d2f1a Mon Sep 17 00:00:00 2001 From: chrysn Date: Thu, 31 Aug 2023 21:41:39 +0200 Subject: [PATCH] demo server: Add index, even with XHTML --- server.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/server.py b/server.py index f0dcda7b..ae89c9c1 100755 --- a/server.py +++ b/server.py @@ -14,8 +14,31 @@ import asyncio import aiocoap.resource as resource +from aiocoap.numbers.contentformat import ContentFormat import aiocoap +class Welcome(resource.Resource): + representations = { + ContentFormat.TEXT: b"Welcome to the demo server", + ContentFormat.LINKFORMAT: b",ct=40", + # ad-hoc for application/xhtml+xml;charset=utf-8 + ContentFormat(65000): + b'' + b'aiocoap demo' + b'

Welcome to the aiocoap demo server!

' + b'', + } + + default_representation = ContentFormat.TEXT + + async def render_get(self, request): + cf = self.default_representation if request.opt.accept is None else request.opt.accept + try: + return aiocoap.Message(payload=self.representations[cf], content_format=cf) + except KeyError: + raise aiocoap.error.UnsupportedContentFormat class BlockResource(resource.Resource): """Example resource which supports the GET and PUT methods. It sends large @@ -117,6 +140,7 @@ async def main(): root.add_resource(['.well-known', 'core'], resource.WKCResource(root.get_resources_as_linkheader)) + root.add_resource([], Welcome()) root.add_resource(['time'], TimeResource()) root.add_resource(['other', 'block'], BlockResource()) root.add_resource(['other', 'separate'], SeparateLargeResource())