Debian Codegraph @

examples/client_ws.py

raw
1 import asyncio
2 import aiorpcx
3
4
5 async def connect(uri):
6 async with aiorpcx.connect_ws(uri) as session:
7 # A good request with standard argument passing
8 result = await session.send_request('echo', ["Howdy"])
9 print(result)
10 # A good request with named argument passing
11 result = await session.send_request('echo', {'message': "Hello with a named argument"})
12 print(result)
13
14 # aiorpcX transparently handles erroneous calls server-side, returning appropriate
15 # errors. This in turn causes an exception to be raised in the client.
16 for bad_args in (
17 ['echo'],
18 ['echo', {}],
19 ['foo'],
20 # This causes an error running the server's buggy request handler.
21 # aiorpcX catches the problem, returning an 'internal server error' to the
22 # client, and continues serving
23 ['sum', [2, 4, "b"]]
24 ):
25 try:
26 await session.send_request(*bad_args)
27 except Exception as exc:
28 print('Send reuest exception:', repr(exc))
29
30 # Batch requests
31 async with session.send_batch() as batch:
32 batch.add_request('echo', ["Me again"])
33 batch.add_notification('ping')
34 batch.add_request('sum', list(range(50)))
35
36 for n, result in enumerate(batch.results, start=1):
37 print(f'batch result #{n}: {result}')
38
39
40 asyncio.get_event_loop().run_until_complete(connect('ws://localhost:8889'))