extras.websocket — Simple Websocket Server (Deprecated)


An HTTP YellowService used to easily mock and test websocket connections.

Deprecated since version 0.6.6: Use webserver instead.

Note

Requires the websocket extra. For more information, see our installation guide.

Setting up an example websocket server
service = WebsocketService()
# Let's write "Hello!" to the websocket upon connection to the "/hello"
# endpoint:
service.add("Hello!", "/hello")
# We'll also make a simple echo endpoint:
@service.route("/echo")
def echo(websocket):
    data = None
    while True:
        # Yield sends out the data, and waits for incoming data.
        data = yield data

service.start()
class extras.websocket.WebsocketService[source]

A Simple WS server wrapped as YellowService.

Inherits from YellowService.

port: int[source]

Read-only attribute denoting the websocket port of the service.

property local_url: str[source]

Full URL to access the WS Server from localhost (including port).

property container_url: str[source]

Full URL to access the WS Server from inside a locally-running container (including port).

add(side_effect, path=None, *, regex=None)[source]

Add a new route to the service.

Parameters:
  • side_effect

    The response for when the route is accessed, can be one of:

    • A primitive value, one of :str, bytes, bytearray or memoryview, which will be sent to the client and the connection closed.

    • The None primiitve, indicating that the websocket should close.

    • An iterable of any combination of the above primitive types, which will be sent one by one to the client, waiting for messages between any two.

    • A Callable which will be called with the Simple Websocket object as the argument. The function should return a primitive value, the value will be sent to the client and the connection closed.

    • A Callable which returns a generator. The callable will be called with the Simple Websocket object as the argument. The generator should yield primitive values, which will be sent to the client and between receiving data.

  • path (str | None) – The path to match the route against. Omit if using regex.

  • regex (str | Pattern[str] | None) – The path pattern to match the route against. Omit if using path.

Note

exactly one of path or regex must be specified.

Raises:

RuntimeError – If the path already exists.

route(path=None, *, regex=None)[source]

A decorator to add a route to the service.

@service.route("/echo")
def echo(websocket):
    data = None
    while True:
        data = yield data

path and regex parameters are the same as add().

set(side_effect, path=None, *, regex=None)[source]

Set a new route or replace an existing route to the service.

Parameters are the same as add().

patch(side_effect, path=None, regex=None) ContextManager[source]

returns a context manager that adds a route to the service on entry and removes it on exit.

Parameters are the same as add().

remove(path=None, *, regex=None)[source]

Remove a route from the service.

Parameters:
  • path (str | None) – The path of the route that was previously inserted. Omit if using regex.

  • regex (str | Pattern[str] | None) – The path pattern of the route that was previously inserted. Omit if using path.

Note

exactly one of path or regex must be specified.

Raises:

KeyError – if the route is not found.

clear()[source]

Remove all routes from the service.