Skip to content

Commit 9729e63

Browse files
committed
Add kwargs to connect() and serve().
Reorganize their docstrings for clarity. Refs #112.
1 parent 1b4f020 commit 9729e63

File tree

4 files changed

+55
-31
lines changed

4 files changed

+55
-31
lines changed

docs/api.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ Server
3232

3333
.. automodule:: websockets.server
3434

35-
.. autofunction:: serve(ws_handler, host=None, port=None, *, loop=None, klass=WebSocketServerProtocol, origins=None, subprotocols=None, extra_headers=None, **kwds)
35+
.. autofunction:: serve(ws_handler, host=None, port=None, *, klass=WebSocketServerProtocol, timeout=10, max_size=2 ** 20, max_queue=2 ** 5, loop=None, origins=None, subprotocols=None, extra_headers=None, **kwds)
3636

37-
.. autoclass:: WebSocketServerProtocol(ws_handler, ws_server, *, origins=None, subprotocols=None, extra_headers=None, host=None, port=None, secure=None, timeout=10, max_size=2 ** 20, loop=None)
37+
.. autoclass:: WebSocketServerProtocol(ws_handler, ws_server, *, host=None, port=None, secure=None, timeout=10, max_size=2 ** 20, max_queue=2 ** 5, loop=None, origins=None, subprotocols=None, extra_headers=None)
3838

3939
.. automethod:: handshake(origins=None, subprotocols=None, extra_headers=None)
4040
.. automethod:: select_subprotocol(client_protos, server_protos)
@@ -44,9 +44,9 @@ Client
4444

4545
.. automodule:: websockets.client
4646

47-
.. autofunction:: connect(uri, *, loop=None, klass=WebSocketClientProtocol, origin=None, subprotocols=None, extra_headers=None, **kwds)
47+
.. autofunction:: connect(uri, *, klass=WebSocketClientProtocol, timeout=10, max_size=2 ** 20, max_queue=2 ** 5, loop=None, origin=None, subprotocols=None, extra_headers=None, **kwds)
4848

49-
.. autoclass:: WebSocketClientProtocol(*, host=None, port=None, secure=None, timeout=10, max_size=2 ** 20, loop=None)
49+
.. autoclass:: WebSocketClientProtocol(*, host=None, port=None, secure=None, timeout=10, max_size=2 ** 20, max_queue=2 ** 5, loop=None)
5050

5151
.. automethod:: handshake(wsuri, origin=None, subprotocols=None, extra_headers=None)
5252

docs/changelog.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ Changelog
44
3.1
55
...
66

7+
* Added ``timeout``, ``max_size``, and ``max_queue`` arguments to
8+
:func:`~websockets.client.connect()` and :func:`~websockets.server.serve()`.
9+
710
* Avoided a warning when closing a connection before the opening handshake.
811

912
* Added flow control for incoming data.

websockets/client.py

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -101,36 +101,44 @@ def handshake(self, wsuri,
101101

102102
@asyncio.coroutine
103103
def connect(uri, *,
104-
loop=None, klass=WebSocketClientProtocol, legacy_recv=False,
104+
klass=WebSocketClientProtocol,
105+
timeout=10, max_size=2 ** 20, max_queue=2 ** 5,
106+
loop=None, legacy_recv=False,
105107
origin=None, subprotocols=None, extra_headers=None,
106108
**kwds):
107109
"""
108-
This coroutine connects to a WebSocket server.
110+
This coroutine connects to a WebSocket server at a given ``uri``.
109111
110-
It's a wrapper around the event loop's
112+
It yields a :class:`WebSocketClientProtocol` which can then be used to
113+
send and receive messages.
114+
115+
:func:`connect` is a wrapper around the event loop's
111116
:meth:`~asyncio.BaseEventLoop.create_connection` method. Extra keyword
112117
arguments are passed to :meth:`~asyncio.BaseEventLoop.create_connection`.
118+
113119
For example, you can set the ``ssl`` keyword argument to a
114120
:class:`~ssl.SSLContext` to enforce some TLS settings. When connecting to
115121
a ``wss://`` URI, if this argument isn't provided explicitly, it's set to
116122
``True``, which means Python's default :class:`~ssl.SSLContext` is used.
117123
118-
:func:`connect` accepts several optional arguments:
124+
The behavior of the ``timeout``, ``max_size``, and ``max_queue`` optional
125+
arguments is described the documentation of
126+
:class:`~websockets.protocol.WebSocketCommonProtocol`.
127+
128+
:func:`connect` also accepts the following optional arguments:
119129
120130
* ``origin`` sets the Origin HTTP header
121131
* ``subprotocols`` is a list of supported subprotocols in order of
122132
decreasing preference
123133
* ``extra_headers`` sets additional HTTP request headers – it can be a
124134
mapping or an iterable of (name, value) pairs
125135
126-
:func:`connect` yields a :class:`WebSocketClientProtocol` which can then
127-
be used to send and receive messages.
128-
129-
It raises :exc:`~websockets.uri.InvalidURI` if ``uri`` is invalid and
130-
:exc:`~websockets.handshake.InvalidHandshake` if the handshake fails.
136+
:func:`connect` raises :exc:`~websockets.uri.InvalidURI` if ``uri`` is
137+
invalid and :exc:`~websockets.handshake.InvalidHandshake` if the opening
138+
handshake fails.
131139
132-
On Python 3.5, it can be used as a asynchronous context manager. In that
133-
case, the connection is closed when exiting the context.
140+
On Python 3.5, :func:`connect` can be used as a asynchronous context
141+
manager. In that case, the connection is closed when exiting the context.
134142
135143
"""
136144
if loop is None:
@@ -144,7 +152,9 @@ def connect(uri, *,
144152
"Use a wss:// URI to enable TLS.")
145153
factory = lambda: klass(
146154
host=wsuri.host, port=wsuri.port, secure=wsuri.secure,
147-
loop=loop, legacy_recv=legacy_recv)
155+
timeout=timeout, max_size=max_size, max_queue=max_queue,
156+
loop=loop, legacy_recv=legacy_recv,
157+
)
148158

149159
transport, protocol = yield from loop.create_connection(
150160
factory, wsuri.host, wsuri.port, **kwds)

websockets/server.py

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -245,22 +245,37 @@ def wait_closed(self):
245245

246246
@asyncio.coroutine
247247
def serve(ws_handler, host=None, port=None, *,
248-
loop=None, klass=WebSocketServerProtocol, legacy_recv=False,
248+
klass=WebSocketServerProtocol,
249+
timeout=10, max_size=2 ** 20, max_queue=2 ** 5,
250+
loop=None, legacy_recv=False,
249251
origins=None, subprotocols=None, extra_headers=None,
250252
**kwds):
251253
"""
252254
This coroutine creates a WebSocket server.
253255
254-
It's a wrapper around the event loop's
255-
:meth:`~asyncio.BaseEventLoop.create_server` method. ``host``, ``port`` as
256-
well as extra keyword arguments are passed to
257-
:meth:`~asyncio.BaseEventLoop.create_server`. For example, you can set the
258-
``ssl`` keyword argument to a :class:`~ssl.SSLContext` to enable TLS.
256+
It yields a :class:`~asyncio.Server` which provides:
257+
258+
* a :meth:`~asyncio.Server.close` method that closes open connections with
259+
status code 1001 and stops accepting new connections
260+
* a :meth:`~asyncio.Server.wait_closed` coroutine that waits until closing
261+
handshakes complete and connections are closed.
259262
260263
``ws_handler`` is the WebSocket handler. It must be a coroutine accepting
261264
two arguments: a :class:`WebSocketServerProtocol` and the request URI.
262265
263-
:func:`serve` accepts several optional arguments:
266+
:func:`serve` is a wrapper around the event loop's
267+
:meth:`~asyncio.BaseEventLoop.create_server` method. ``host``, ``port`` as
268+
well as extra keyword arguments are passed to
269+
:meth:`~asyncio.BaseEventLoop.create_server`.
270+
271+
For example, you can set the ``ssl`` keyword argument to a
272+
:class:`~ssl.SSLContext` to enable TLS.
273+
274+
The behavior of the ``timeout``, ``max_size``, and ``max_queue`` optional
275+
arguments is described the documentation of
276+
:class:`~websockets.protocol.WebSocketCommonProtocol`.
277+
278+
:func:`serve` also accepts the following optional arguments:
264279
265280
* ``origins`` defines acceptable Origin HTTP headers — include
266281
``''`` if the lack of an origin is acceptable
@@ -270,13 +285,6 @@ def serve(ws_handler, host=None, port=None, *,
270285
mapping, an iterable of (name, value) pairs, or a callable taking the
271286
request path and headers in arguments.
272287
273-
:func:`serve` yields a :class:`~asyncio.Server` which provides:
274-
275-
* a :meth:`~asyncio.Server.close` method that closes open connections with
276-
status code 1001 and stops accepting new connections
277-
* a :meth:`~asyncio.Server.wait_closed` coroutine that waits until closing
278-
handshakes complete and connections are closed.
279-
280288
Whenever a client connects, the server accepts the connection, creates a
281289
:class:`WebSocketServerProtocol`, performs the opening handshake, and
282290
delegates to the WebSocket handler. Once the handler completes, the server
@@ -301,8 +309,11 @@ def serve(ws_handler, host=None, port=None, *,
301309
factory = lambda: klass(
302310
ws_handler, ws_server,
303311
host=host, port=port, secure=secure,
312+
timeout=timeout, max_size=max_size, max_queue=max_queue,
313+
loop=loop, legacy_recv=legacy_recv,
304314
origins=origins, subprotocols=subprotocols,
305-
extra_headers=extra_headers, loop=loop, legacy_recv=legacy_recv)
315+
extra_headers=extra_headers,
316+
)
306317
server = yield from loop.create_server(factory, host, port, **kwds)
307318

308319
ws_server.wrap(server)

0 commit comments

Comments
 (0)