File: clientGET-observe.py

package info (click to toggle)
aiocoap 0.4.17-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 2,040 kB
  • sloc: python: 17,241; makefile: 23; sh: 9
file content (39 lines) | stat: -rwxr-xr-x 934 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#!/usr/bin/env python3

# SPDX-FileCopyrightText: Christian Amsüss and the aiocoap contributors
#
# SPDX-License-Identifier: MIT

"""This is a usage example of aiocoap that demonstrates how to implement a
simple client. See the "Usage Examples" section in the aiocoap documentation
for some more information."""

import logging
import asyncio

from aiocoap import *

logging.basicConfig(level=logging.INFO)


async def main():
    protocol = await Context.create_client_context()

    request = Message(code=GET, uri="coap://vs0.inf.ethz.ch/obs", observe=0)

    pr = protocol.request(request)

    r = await pr.response
    print("First response: %s\n%r" % (r, r.payload))

    async for r in pr.observation:
        print("Next result: %s\n%r" % (r, r.payload))

        pr.observation.cancel()
        break
    print("Loop ended, sticking around")
    await asyncio.sleep(50)


if __name__ == "__main__":
    asyncio.run(main())