Provide some utility functions useful for implementing different components in bokeh.server.
bokeh.server
bind_sockets
Bind a socket to a port on an address.
address (str) – An address to bind a port on, e.g. "localhost"
"localhost"
port (int) –
A port number to bind.
Pass 0 to have the OS automatically choose a free port.
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.)
(socket, port)
check_allowlist
Check a given request host against a allowlist.
host (str) –
A host string to compare against a allowlist.
If the host does not specify a port, then ":80" is implicitly assumed.
":80"
allowlist (seq[str]) – A list of host patterns to match against
True, if host matches any pattern in allowlist, otherwise False
True
host
allowlist
False
create_hosts_allowlist
This allowlist can be used to restrict websocket or other connections to only those explicitly originating from approved hosts.
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.
If host_list is empty or None, then the allowlist will be the single item list `` [ ‘localhost:<port>’ ]``
host_list
None
If host_list is not empty, this parameter has no effect.
list[str]
ValueError, if host or port values are invalid –
Note
If any host in host_list contains a wildcard * a warning will be logged regarding permissive websocket connections.
*
match_host
Match a host string against a pattern
host (str) – A hostname to compare to the given pattern
pattern (str) – A string representing a hostname pattern, possibly including wildcards for ip address octets or ports.
This function will return True if the hostname matches the pattern, including any wildcards. If the pattern contains a port, the host string must also contain a matching port.
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('foo.example.com', 'foo.example.com.net') 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