INNER CODE UNIT · Python
parse_protocol
selkies-project/selkies · addons/turn-rest/app.py:81
def parse_protocol(value, fallback: str = 'udp') -> str:
"""Normalize a transport protocol string to ``tcp`` or ``udp``."""
candidate = (value or fallback or 'udp').strip().lower()
return 'tcp' if candidate == 'tcp' else 'udp'
def format_ice_host(host: str) -> str:
"""Bracket a bare IPv6 literal so it is valid inside a TURN URI."""
if host and ":" in host and not (host.startswith("[") and host.endswith("]")):
return f"[{host}]"
return host
def random_username(length: int = 16) -> str:
"""A random lowercase-alphanumeric username for anonymous requests."""
alphabet = string.ascii_lowercase + string.digits
return ''.join(secrets.choice(alphabet) for _ in range(length))