aiocoap icon indicating copy to clipboard operation
aiocoap copied to clipboard

how can I catch timeouterror?

Open scarleast opened this issue 5 years ago • 0 comments

I want to use this lib as a client, to check if my coap server work normal.but I can't catch any error when it timeout!

import asyncio
from aiocoap import *
import aiocoap.error


async def main():
    """Perform a single PUT request to localhost on the default port, URI
    "/other/block". The request is sent 2 seconds after initialization.

    The payload is bigger than 1kB, and thus sent as several blocks."""

    context = await Context.create_client_context()

    await asyncio.sleep(2)

    payload = b"The quick brown fox jumps over the lazy dog.\n"
    request = Message(code=3, payload=payload, uri="coap://localhost/other/block")

    try:
        response = await context.request(request).response
        print('Result: %s\n%r' % (response.code, response.payload))
    except aiocoap.error.Error as e:
        print(e)


if __name__ == "__main__":
    asyncio.get_event_loop().run_until_complete(main())

scarleast avatar May 08 '19 07:05 scarleast