Codebase list aiocoap / run/22045c38-c300-45a1-9b4a-6f3fbcd81559/main clientGET.py
run/22045c38-c300-45a1-9b4a-6f3fbcd81559/main

Tree @run/22045c38-c300-45a1-9b4a-6f3fbcd81559/main (Download .tar.gz)

clientGET.py @run/22045c38-c300-45a1-9b4a-6f3fbcd81559/mainraw · history · blame

#!/usr/bin/env python3

# This file is part of the Python aiocoap library project.
#
# Copyright (c) 2012-2014 Maciej Wasilak <http://sixpinetrees.blogspot.com/>,
#               2013-2014 Christian Amsüss <c.amsuess@energyharvesting.at>
#
# aiocoap is free software, this file is published under the MIT license as
# described in the accompanying LICENSE file.

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://localhost/time')

    try:
        response = await protocol.request(request).response
    except Exception as e:
        print('Failed to fetch resource:')
        print(e)
    else:
        print('Result: %s\n%r'%(response.code, response.payload))

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