MCPcopy Index your code
hub / github.com/python-websockets/websockets / build_host

Function build_host

src/websockets/headers.py:39–65  ·  view source on GitHub ↗

Build a ``Host`` header.

(
    host: str,
    port: int,
    secure: bool,
    *,
    always_include_port: bool = False,
)

Source from the content-addressed store, hash-verified

37
38
39def build_host(
40 host: str,
41 port: int,
42 secure: bool,
43 *,
44 always_include_port: bool = False,
45) -> str:
46 """
47 Build a ``Host`` header.
48
49 """
50 # https://datatracker.ietf.org/doc/html/rfc3986#section-3.2.2
51 # IPv6 addresses must be enclosed in brackets.
52 try:
53 address = ipaddress.ip_address(host)
54 except ValueError:
55 # host is a hostname
56 pass
57 else:
58 # host is an IP address
59 if address.version == 6:
60 host = f"[{host}]"
61
62 if always_include_port or port != (443 if secure else 80):
63 host = f"{host}:{port}"
64
65 return host
66
67
68# To avoid a dependency on a parsing library, we implement manually the ABNF

Callers 4

connectMethod · 0.85
prepare_connect_requestFunction · 0.85
handshakeMethod · 0.85
test_build_hostMethod · 0.85

Calls

no outgoing calls

Tested by 1

test_build_hostMethod · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…