Create a WebSocket server listening on ``host`` and ``port``. Whenever a client connects, the server creates a :class:`ServerConnection`, performs the opening handshake, and delegates to the ``handler`` coroutine. The handler receives the :class:`ServerConnection` instance, which
| 576 | |
| 577 | # This is spelled in lower case because it's exposed as a callable in the API. |
| 578 | class serve: |
| 579 | """ |
| 580 | Create a WebSocket server listening on ``host`` and ``port``. |
| 581 | |
| 582 | Whenever a client connects, the server creates a :class:`ServerConnection`, |
| 583 | performs the opening handshake, and delegates to the ``handler`` coroutine. |
| 584 | |
| 585 | The handler receives the :class:`ServerConnection` instance, which you can |
| 586 | use to send and receive messages. |
| 587 | |
| 588 | Once the handler completes, either normally or with an exception, the server |
| 589 | performs the closing handshake and closes the connection. |
| 590 | |
| 591 | This coroutine returns a :class:`Server` whose API mirrors |
| 592 | :class:`asyncio.Server`. Treat it as an asynchronous context manager to |
| 593 | ensure that the server will be closed:: |
| 594 | |
| 595 | from websockets.asyncio.server import serve |
| 596 | |
| 597 | def handler(websocket): |
| 598 | ... |
| 599 | |
| 600 | # set this future to exit the server |
| 601 | stop = asyncio.get_running_loop().create_future() |
| 602 | |
| 603 | async with serve(handler, host, port): |
| 604 | await stop |
| 605 | |
| 606 | Alternatively, call :meth:`~Server.serve_forever` to serve requests and |
| 607 | cancel it to stop the server:: |
| 608 | |
| 609 | server = await serve(handler, host, port) |
| 610 | await server.serve_forever() |
| 611 | |
| 612 | Args: |
| 613 | handler: Connection handler. It receives the WebSocket connection, |
| 614 | which is a :class:`ServerConnection`, in argument. |
| 615 | host: Network interfaces the server binds to. |
| 616 | See :meth:`~asyncio.loop.create_server` for details. |
| 617 | port: TCP port the server listens on. |
| 618 | See :meth:`~asyncio.loop.create_server` for details. |
| 619 | origins: Acceptable values of the ``Origin`` header, for defending |
| 620 | against Cross-Site WebSocket Hijacking attacks. Values can be |
| 621 | :class:`str` to test for an exact match or regular expressions |
| 622 | compiled by :func:`re.compile` to test against a pattern. Include |
| 623 | :obj:`None` in the list if the lack of an origin is acceptable. |
| 624 | extensions: List of supported extensions, in order in which they |
| 625 | should be negotiated and run. |
| 626 | subprotocols: List of supported subprotocols, in order of decreasing |
| 627 | preference. |
| 628 | select_subprotocol: Callback for selecting a subprotocol among |
| 629 | those supported by the client and the server. It receives a |
| 630 | :class:`ServerConnection` (not a |
| 631 | :class:`~websockets.server.ServerProtocol`!) instance and a list of |
| 632 | subprotocols offered by the client. Other than the first argument, |
| 633 | it has the same behavior as the |
| 634 | :meth:`ServerProtocol.select_subprotocol |
| 635 | <websockets.server.ServerProtocol.select_subprotocol>` method. |
no outgoing calls
no test coverage detected
searching dependent graphs…