Skip to content

Commit dd7dbd1

Browse files
committed
demo server: Add index, even with XHTML
1 parent 72f1064 commit dd7dbd1

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

server.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,31 @@
1414
import asyncio
1515

1616
import aiocoap.resource as resource
17+
from aiocoap.numbers.contentformat import ContentFormat
1718
import aiocoap
1819

20+
class Welcome(resource.Resource):
21+
representations = {
22+
ContentFormat.TEXT: b"Welcome to the demo server",
23+
ContentFormat.LINKFORMAT: b"</.well-known/core>,ct=40",
24+
# ad-hoc for application/xhtml+xml;charset=utf-8
25+
ContentFormat(65000):
26+
b'<html xmlns="http://www.w3.org/1999/xhtml">'
27+
b'<head><title>aiocoap demo</title></head>'
28+
b'<body><h1>Welcome to the aiocoap demo server!</h1>'
29+
b'<ul><li><a href="time">Current time</a></li>'
30+
b'<li><a href="whoami">Report my network address</a></li>'
31+
b'</ul></body></html>',
32+
}
33+
34+
default_representation = ContentFormat.TEXT
35+
36+
async def render_get(self, request):
37+
cf = self.default_representation if request.opt.accept is None else request.opt.accept
38+
try:
39+
return aiocoap.Message(payload=self.representations[cf], content_format=cf)
40+
except KeyError:
41+
raise aiocoap.error.UnsupportedContentFormat
1942

2043
class BlockResource(resource.Resource):
2144
"""Example resource which supports the GET and PUT methods. It sends large
@@ -117,6 +140,7 @@ async def main():
117140

118141
root.add_resource(['.well-known', 'core'],
119142
resource.WKCResource(root.get_resources_as_linkheader))
143+
root.add_resource([], Welcome())
120144
root.add_resource(['time'], TimeResource())
121145
root.add_resource(['other', 'block'], BlockResource())
122146
root.add_resource(['other', 'separate'], SeparateLargeResource())

0 commit comments

Comments
 (0)