bokeh.server.util#
Provide some utility functions useful for implementing different
components in bokeh.server.
- bind_sockets(address: str | None, port: int) tuple[list[socket], int][source]#
Bind a socket to a port on an address.
- Parameters:
This function returns a 2-tuple with the new socket as the first element, and the port that was bound as the second. (Useful when passing 0 as a port number to bind any free port.)
- Returns:
(socket, port)
- check_allowlist(host: str, allowlist: Sequence[str]) bool[source]#
Check a given request host against a allowlist.
- create_hosts_allowlist(host_list: Sequence[str] | None, port: int | None) list[str][source]#
This allowlist can be used to restrict websocket or other connections to only those explicitly originating from approved hosts.
- Parameters:
host_list (seq[str]) –
A list of string <name> or <name>:<port> values to add to the allowlist.
If no port is specified in a host string, then
":80"is implicitly assumed.port (int) –
If
host_listis empty orNone, then the allowlist will be the single item list `` [ ‘localhost:<port>’ ]``If
host_listis not empty, this parameter has no effect.
- Returns:
list[str]
- Raises:
ValueError, if host or port values are invalid –
Note
If any host in
host_listcontains a wildcard*a warning will be logged regarding permissive websocket connections.
- match_host(host: str, pattern: str) bool[source]#
Match a host string against a pattern
- Parameters:
This function will return
Trueif the hostname matches the pattern, including any wildcards. If the pattern does not include any wildcards, then the length the host parts and pattern parts must match identically. If the pattern contains a port, the host string must also contain a matching port.- Returns:
bool
Examples
>>> match_host('192.168.0.1:80', '192.168.0.1:80') True >>> match_host('192.168.0.1:80', '192.168.0.1') True >>> match_host('192.168.0.1:80', '192.168.0.1:8080') False >>> match_host('192.168.0.1', '192.168.0.2') False >>> match_host('192.168.0.1', '192.168.*.*') True >>> match_host('alice', 'alice') True >>> match_host('alice:80', 'alice') True >>> match_host('alice', 'bob') False >>> match_host('example.com', 'example.com.net') False >>> match_host('example.com.bad.com', 'example.com') False >>> match_host('alice', '*') True >>> match_host('alice', '*:*') True >>> match_host('alice:80', '*') True >>> match_host('alice:80', '*:80') True >>> match_host('alice:8080', '*:80') False