MCPcopy Create free account
hub / github.com/python-websockets/websockets / Server

Class Server

src/websockets/asyncio/server.py:228–574  ·  view source on GitHub ↗

WebSocket server returned by :func:`serve`. This class mirrors the API of :class:`asyncio.Server`. It keeps track of WebSocket connections in order to close them properly when shutting down. Args: handler: Connection handler. It receives the WebSocket connection,

Source from the content-addressed store, hash-verified

226
227
228class Server:
229 """
230 WebSocket server returned by :func:`serve`.
231
232 This class mirrors the API of :class:`asyncio.Server`.
233
234 It keeps track of WebSocket connections in order to close them properly
235 when shutting down.
236
237 Args:
238 handler: Connection handler. It receives the WebSocket connection,
239 which is a :class:`ServerConnection`, in argument.
240 process_request: Intercept the request during the opening handshake.
241 Return an HTTP response to force the response. Return :obj:`None` to
242 continue normally. When you force an HTTP 101 Continue response, the
243 handshake is successful. Else, the connection is aborted.
244 ``process_request`` may be a function or a coroutine.
245 process_response: Intercept the response during the opening handshake.
246 Modify the response or return a new HTTP response to force the
247 response. Return :obj:`None` to continue normally. When you force an
248 HTTP 101 Continue response, the handshake is successful. Else, the
249 connection is aborted. ``process_response`` may be a function or a
250 coroutine.
251 server_header: Value of the ``Server`` response header.
252 It defaults to ``"Python/x.y.z websockets/X.Y"``. Setting it to
253 :obj:`None` removes the header.
254 open_timeout: Timeout for opening connections in seconds.
255 :obj:`None` disables the timeout.
256 logger: Logger for this server.
257 It defaults to ``logging.getLogger("websockets.server")``.
258 See the :doc:`logging guide <../../topics/logging>` for details.
259
260 """
261
262 def __init__(
263 self,
264 handler: Callable[[ServerConnection], Awaitable[None]],
265 *,
266 process_request: (
267 Callable[
268 [ServerConnection, Request],
269 Awaitable[Response | None] | Response | None,
270 ]
271 | None
272 ) = None,
273 process_response: (
274 Callable[
275 [ServerConnection, Request, Response],
276 Awaitable[Response | None] | Response | None,
277 ]
278 | None
279 ) = None,
280 server_header: str | None = SERVER,
281 open_timeout: float | None = 10,
282 logger: LoggerLike | None = None,
283 ) -> None:
284 self.loop = asyncio.get_running_loop()
285 self.handler = handler

Callers 1

__init__Method · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…