| 1 |
.. currentmodule:: aiorpcx |
| 2 |
|
| 3 |
Exceptions |
| 4 |
---------- |
| 5 |
|
| 6 |
.. exception:: ConnectionError |
| 7 |
|
| 8 |
When a connection is lost that has pending requests, this exception is set on |
| 9 |
those requests. |
| 10 |
|
| 11 |
|
| 12 |
Server |
| 13 |
====== |
| 14 |
|
| 15 |
A simple wrapper around an :class:`asyncio.Server` object (see |
| 16 |
`asyncio.Server |
| 17 |
<https://docs.python.org/3/library/asyncio-eventloop.html#asyncio.Server>`_). |
| 18 |
|
| 19 |
.. class:: Server(protocol_factory, host=None, port=None, *, loop=None, \ |
| 20 |
**kwargs) |
| 21 |
|
| 22 |
Creates a server that listens for connections on *host* and *port*. |
| 23 |
The server does not actually start listening until :meth:`listen` is |
| 24 |
await-ed. |
| 25 |
|
| 26 |
*protocol_factory* is any callable returning an |
| 27 |
:class:`asyncio.Protocol` instance. You might find returning an |
| 28 |
instance of :class:`ServerSession`, or a class derived from it, more |
| 29 |
useful. |
| 30 |
|
| 31 |
*loop* is the event loop to use, or :func:`asyncio.get_event_loop()` |
| 32 |
if :const:`None`. |
| 33 |
|
| 34 |
*kwargs* are passed through to `loop.create_server()`_. |
| 35 |
|
| 36 |
A server instance has the following attributes: |
| 37 |
|
| 38 |
.. attribute:: loop |
| 39 |
|
| 40 |
The event loop being used. |
| 41 |
|
| 42 |
.. attribute:: host |
| 43 |
|
| 44 |
The host passed to the constructor |
| 45 |
|
| 46 |
.. attribute:: port |
| 47 |
|
| 48 |
The port passed to the constructor |
| 49 |
|
| 50 |
.. attribute:: server |
| 51 |
|
| 52 |
The underlying :class:`asyncio.Server` object when the server is |
| 53 |
listening, otherwise :const:`None`. |
| 54 |
|
| 55 |
.. method:: listen() |
| 56 |
|
| 57 |
Start listening for incoming connections. Return an |
| 58 |
:class:`asyncio.Server` instance, which can also be accessed via |
| 59 |
:attr:`server`. |
| 60 |
|
| 61 |
This method is a `coroutine`_. |
| 62 |
|
| 63 |
.. method:: close() |
| 64 |
|
| 65 |
Close the listening socket if the server is listening, and wait |
| 66 |
for it to close. Return immediately if the server is not |
| 67 |
listening. |
| 68 |
|
| 69 |
This does nothing to protocols and transports handling existing |
| 70 |
connections. On return :attr:`server` is :const:`None`. |
| 71 |
|
| 72 |
.. method:: wait_closed() |
| 73 |
|
| 74 |
Returns when the server has closed. |
| 75 |
|
| 76 |
This method is a `coroutine`_. |
| 77 |
|
| 78 |
Sessions |
| 79 |
======== |
| 80 |
|
| 81 |
Convenience classes are provided for client and server sessions. |
| 82 |
|
| 83 |
|
| 84 |
.. class:: ClientSession(host, port, *, rpc_protocol=None, framer=None, \ |
| 85 |
scheduler=None, loop=None, proxy=None, **kwargs) |
| 86 |
|
| 87 |
An instance of an :class:`asyncio.Protocol` class that represents an |
| 88 |
RPC session with a remote server at *host* and *port*, as documented |
| 89 |
in `loop.create_connection()`_.` |
| 90 |
|
| 91 |
If *proxy* is not given, :meth:`create_connection` uses |
| 92 |
:meth:`loop.create_connection` to attempt a connection, otherwise |
| 93 |
:meth:`SOCKSProxy.create_connection`. You can pass additional |
| 94 |
arguments to those functions with *kwargs* (*host* and *port* and |
| 95 |
*loop* are used as given). |
| 96 |
|
| 97 |
*rpc_protocol* specifies the RPC protocol the server speaks. If |
| 98 |
:const:`None` the protocol returned by :meth:`default_rpc_protocol` |
| 99 |
is used. |
| 100 |
|
| 101 |
*framer* handles RPC message framing, and if :const:`None` then the |
| 102 |
framer returned by :meth:`default_framer` is used. |
| 103 |
|
| 104 |
*scheduler* should be left as :const:`None`. |
| 105 |
|
| 106 |
Logging will be sent to *logger*, :const:`None` will use a logger |
| 107 |
specific to the :class:`ClientSession` object's class. |
| 108 |
|
| 109 |
.. method:: create_connection() |
| 110 |
|
| 111 |
Make a connection attempt to the remote server. If successful |
| 112 |
this return a ``(transport, protocol)`` pair. |
| 113 |
|
| 114 |
This method is a `coroutine`_. |
| 115 |
|
| 116 |
.. method:: default_rpc_protocol() |
| 117 |
|
| 118 |
You can override this method to provide a default RPC protocol. |
| 119 |
:class:`JSONRPCv2` is returned by the default implementation. |
| 120 |
|
| 121 |
.. method:: default_framer() |
| 122 |
|
| 123 |
You can override this method to provide a default message frmaer. |
| 124 |
A new :class:`NewlineFramer` instance is returned by the default |
| 125 |
implementation. |
| 126 |
|
| 127 |
|
| 128 |
The :class:`ClientSession` and :class:`ServerSession` classes share a |
| 129 |
base class that has the following attributes and methods: |
| 130 |
|
| 131 |
|
| 132 |
.. _loop.create_connection(): |
| 133 |
https://docs.python.org/3/library/asyncio-eventloop.html#asyncio.AbstractEventLoop.create_connection |
| 134 |
.. _loop.create_server(): |
| 135 |
https://docs.python.org/3/library/asyncio-eventloop.html#asyncio.AbstractEventLoop.create_server |
| 136 |
|
| 137 |
.. _coroutine: |
| 138 |
https://docs.python.org/3/library/asyncio-task.html#coroutine |