|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import logging |
| 4 | +import asyncio |
| 5 | +import sys |
| 6 | +from argparse import ArgumentParser, Namespace |
| 7 | + |
| 8 | +from aiocoap import * |
| 9 | +import aiocoap.resource as resource |
| 10 | + |
| 11 | +logging.basicConfig(level=logging.DEBUG) |
| 12 | + |
| 13 | +print("usage: client.py -m <GET|PUT|POST|DELETE|PATCH|iPATCH|FETCH> -u <URI> [--type <NON|CON>] [--observe] [-p <PAYLOAD>]") |
| 14 | + |
| 15 | +def _get(_list, i, default): |
| 16 | + return _list[i] if len(_list) > i else default |
| 17 | + |
| 18 | +def message_type(arg): |
| 19 | + return { "CON": CON, "NON": NON }[arg] |
| 20 | + |
| 21 | +def method(arg): |
| 22 | + return { |
| 23 | + "GET": GET, |
| 24 | + "PUT": PUT, |
| 25 | + "POST": POST, |
| 26 | + "DELETE": DELETE, |
| 27 | + "PATCH": PATCH, |
| 28 | + "iPATCH": iPATCH, |
| 29 | + "FETCH": FETCH |
| 30 | + }[arg] |
| 31 | + |
| 32 | +async def main(): |
| 33 | + parser = ArgumentParser() |
| 34 | + |
| 35 | + parser.add_argument( |
| 36 | + "-m", "--method", |
| 37 | + help='GET|PUT|POST|DELETE|PATCH|iPATCH|FETCH', |
| 38 | + required=True) |
| 39 | + |
| 40 | + parser.add_argument( |
| 41 | + "-u", "--uri", |
| 42 | + help='URI', |
| 43 | + required=True) |
| 44 | + |
| 45 | + parser.add_argument( |
| 46 | + "-t", "--type", |
| 47 | + help='NON|CON', |
| 48 | + default="NON") |
| 49 | + |
| 50 | + parser.add_argument( |
| 51 | + "--observe", |
| 52 | + action="store_true", |
| 53 | + help='Register for notifications', |
| 54 | + default=False) |
| 55 | + |
| 56 | + parser.add_argument( |
| 57 | + "--observe-cancel", |
| 58 | + action="store_true", |
| 59 | + help='Cancel notifications', |
| 60 | + default=False) |
| 61 | + |
| 62 | + parser.add_argument( |
| 63 | + "-p", "--payload", |
| 64 | + help='Payload', |
| 65 | + default=None) |
| 66 | + |
| 67 | + args = parser.parse_args() |
| 68 | + |
| 69 | + observeValue = None |
| 70 | + |
| 71 | + if args.observe and args.observe_cancel: |
| 72 | + raise ValueError("Cannot register for and cancel notifications") |
| 73 | + |
| 74 | + if args.observe: |
| 75 | + observeValue = 0 |
| 76 | + |
| 77 | + elif args.observe_cancel: |
| 78 | + observeValue = 1 |
| 79 | + |
| 80 | + print(f"using {message_type(args.type)} {method(args.method)} request"); |
| 81 | + |
| 82 | + port = 5600 |
| 83 | + protocol = await Context.create_server_context(bind=("::", port), site=resource.Site()) |
| 84 | + protocol.client_credentials.load_from_dict({ |
| 85 | + '*': { |
| 86 | + 'dtls': { |
| 87 | + 'psk': b'secretPSK', |
| 88 | + 'client-identity': b'Client_identity', |
| 89 | + } |
| 90 | + } |
| 91 | + }) |
| 92 | + |
| 93 | + request = Message( |
| 94 | + mtype=message_type(args.type), |
| 95 | + code=method(args.method), |
| 96 | + uri=args.uri, |
| 97 | + payload=bytes(args.payload, 'utf-8') if args.payload else "", |
| 98 | + observe=observeValue |
| 99 | + ) |
| 100 | + |
| 101 | + try: |
| 102 | + pr = protocol.request(request) |
| 103 | + |
| 104 | + r = await pr.response |
| 105 | + print("response: %s\n%r" % (r.code, r.payload)) |
| 106 | + |
| 107 | + if args.observe: |
| 108 | + print("waiting for resource notifications") |
| 109 | + async for r in pr.observation: |
| 110 | + print("notification: %s\n%r" % (r, r.payload)) |
| 111 | + break; |
| 112 | + |
| 113 | + except Exception as e: |
| 114 | + print("error:") |
| 115 | + print(e) |
| 116 | + |
| 117 | +if __name__ == "__main__": |
| 118 | + asyncio.run(main()) |
0 commit comments