Skip to content

Commit

Permalink
demo server: Add index, even with XHTML
Browse files Browse the repository at this point in the history
  • Loading branch information
chrysn committed Aug 31, 2023
1 parent 72f1064 commit dd7dbd1
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"</.well-known/core>,ct=40",
# ad-hoc for application/xhtml+xml;charset=utf-8
ContentFormat(65000):
b'<html xmlns="http://www.w3.org/1999/xhtml">'
b'<head><title>aiocoap demo</title></head>'
b'<body><h1>Welcome to the aiocoap demo server!</h1>'
b'<ul><li><a href="time">Current time</a></li>'
b'<li><a href="whoami">Report my network address</a></li>'
b'</ul></body></html>',
}

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
Expand Down Expand Up @@ -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())
Expand Down

0 comments on commit dd7dbd1

Please sign in to comment.