MCPcopy
hub / github.com/encode/httpx / normalize_port

Function normalize_port

httpx/_urlparse.py:395–419  ·  view source on GitHub ↗
(port: str | int | None, scheme: str)

Source from the content-addressed store, hash-verified

393
394
395def normalize_port(port: str | int | None, scheme: str) -> int | None:
396 # From https://tools.ietf.org/html/rfc3986#section-3.2.3
397 #
398 # "A scheme may define a default port. For example, the "http" scheme
399 # defines a default port of "80", corresponding to its reserved TCP
400 # port number. The type of port designated by the port number (e.g.,
401 # TCP, UDP, SCTP) is defined by the URI scheme. URI producers and
402 # normalizers should omit the port component and its ":" delimiter if
403 # port is empty or if its value would be the same as that of the
404 # scheme's default."
405 if port is None or port == "":
406 return None
407
408 try:
409 port_as_int = int(port)
410 except ValueError:
411 raise InvalidURL(f"Invalid port: {port!r}")
412
413 # See https://url.spec.whatwg.org/#url-miscellaneous
414 default_port = {"ftp": 21, "http": 80, "https": 443, "ws": 80, "wss": 443}.get(
415 scheme
416 )
417 if port_as_int == default_port:
418 return None
419 return port_as_int
420
421
422def validate_path(path: str, has_scheme: bool, has_authority: bool) -> None:

Callers 1

urlparseFunction · 0.85

Calls 2

InvalidURLClass · 0.85
getMethod · 0.45

Tested by

no test coverage detected