diff --git a/docs/deployment.rst b/docs/deployment.rst index 0203cfcf9..df6a96f43 100644 --- a/docs/deployment.rst +++ b/docs/deployment.rst @@ -75,4 +75,7 @@ widely different operational characteristics of HTTP and WebSocket. ``websockets`` provide minimal support for responding to HTTP requests with the :meth:`~server.WebSocketServerProtocol.process_request()` hook. Typical -use cases include health checks. +use cases include health checks. Here's an example: + +.. literalinclude:: ../example/health_check_server.py + :emphasize-lines: 7-11,17-18 diff --git a/example/health_check_server.py b/example/health_check_server.py new file mode 100644 index 000000000..13afacdaa --- /dev/null +++ b/example/health_check_server.py @@ -0,0 +1,21 @@ +#!/usr/bin/env python + +import asyncio +import http +import websockets + +class ServerProtocol(websockets.WebSocketServerProtocol): + + async def process_request(self, path, request_headers): + if path == '/__health__/': + return http.HTTPStatus.OK, [], b'OK\n' + +async def echo(websocket, path): + async for message in websocket: + await websocket.send(message) + +start_server = websockets.serve( + echo, 'localhost', 8765, create_protocol=ServerProtocol) + +asyncio.get_event_loop().run_until_complete(start_server) +asyncio.get_event_loop().run_forever()