Skip to content

url

Robust server‑availability checker with built‑in SSRF / safety guards.

ServerCheckResult dataclass Link

ServerCheckResult(url, status_code, ok, final_url, message, n_redirects=0)

Class containing information about the server's response.

is_server_available Link

is_server_available(url, timeout=5.0, max_redirects=2, max_retries=3, backoff_factor=0.3, allow_private_ip=False, validate_host=True, cert_path=None, verify_ssl=True)

Check if a server is available by making an HTTP request to the given URL.

Parameters:

Name Type Description Default

url Link

str

The URL of the server to check.

required

timeout Link

float

Maximum number of seconds to wait for a response. Default is 5.0.

5.0

max_redirects Link

int

Maximum number of redirects to follow. Default is 2.

2

max_retries Link

int

Maximum number of retries in case of failure. Default is 3.

3

backoff_factor Link

float

Multiplier for the delay between retry attempts. Default is 0.3.

0.3

allow_private_ip Link

bool

If True, private or non‑routable IPs are accepted.

False

validate_host Link

bool

Whether to validate the hostname against a whitelist/blacklist. Default is True.

True

cert_path Link

str or Path

Path to a CA bundle to use. Default is None.

None

verify_ssl Link

bool

Whether to verify SSL certificates. Default is True.

True

Returns:

Type Description
ServerCheckResult

An object containing details about the server check result.

Raises:

Type Description
ValueError

If the supplied URL is malformed or a redirect target is invalid.

RuntimeError

If the maximum redirect limit is exceeded.

ConnectionError

If the HTTP request fails due to network issues.

Examples:

>>> from plantdb.client.url import is_server_available
>>> scr = is_server_available('https://google.com', validate_host=False)
>>> print(scr.final_url)
https://www.google.com/
>>> scr = is_server_available('https://google.com', validate_host=True)
INFO     [url] Validating URL hostname: google.com...
INFO     [url] Validating URL hostname: www.google.com...
print(scr.ok)
True
Source code in plantdb/client/url.py
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
def is_server_available(
        url: str,
        timeout: float = 5.0,
        max_redirects: int = 2,
        max_retries: int = 3,
        backoff_factor: float = 0.3,
        allow_private_ip: bool = False,
        validate_host: bool = True,
        cert_path: Optional[str] = None,
        verify_ssl: bool = True,
) -> ServerCheckResult:
    """Check if a server is available by making an HTTP request to the given URL.

    Parameters
    ----------
    url : str
        The URL of the server to check.
    timeout : float, optional
        Maximum number of seconds to wait for a response. Default is ``5.0``.
    max_redirects : int, optional
        Maximum number of redirects to follow. Default is ``2``.
    max_retries : int, optional
        Maximum number of retries in case of failure. Default is ``3``.
    backoff_factor : float, optional
        Multiplier for the delay between retry attempts. Default is ``0.3``.
    allow_private_ip : bool, optional
        If ``True``, private or non‑routable IPs are accepted.
    validate_host : bool, optional
        Whether to validate the hostname against a whitelist/blacklist. Default is ``True``.
    cert_path : str or pathlib.Path, optional
        Path to a CA bundle to use. Default is ``None``.
    verify_ssl : bool, optional
        Whether to verify SSL certificates. Default is ``True``.

    Returns
    -------
    plantdb.client.url.ServerCheckResult
        An object containing details about the server check result.

    Raises
    ------
    ValueError
        If the supplied URL is malformed or a redirect target is invalid.
    RuntimeError
        If the maximum redirect limit is exceeded.
    ConnectionError
        If the HTTP request fails due to network issues.

    Examples
    --------
    >>> from plantdb.client.url import is_server_available
    >>> scr = is_server_available('https://google.com', validate_host=False)
    >>> print(scr.final_url)
    https://www.google.com/
    >>> scr = is_server_available('https://google.com', validate_host=True)
    INFO     [url] Validating URL hostname: google.com...
    INFO     [url] Validating URL hostname: www.google.com...
    print(scr.ok)
    True
    """
    # ------------------------------------------------------------------ #
    # 1. Parse the URL
    # ------------------------------------------------------------------ #
    parsed = _parse_url(url)
    if not parsed:
        message = f"URL '{url}' is not a valid URL."
        logger.warning(message)
        return ServerCheckResult(
            url=url,
            status_code=0,
            ok=False,
            final_url=url,
            message=message,
        )

    # ------------------------------------------------------------------ #
    # 2. Build retry strategy
    # ------------------------------------------------------------------ #
    retry_strategy = Retry(
        total=max_retries,
        backoff_factor=backoff_factor,
        status_forcelist=[500, 502, 503, 504],
        allowed_methods={"GET", "HEAD"},
        redirect=max_redirects,
    )

    # ------------------------------------------------------------------ #
    # 3. Configure SSL
    # ------------------------------------------------------------------ #
    cert_path_obj = Path(cert_path) if cert_path is not None else None
    ssl_context, ca_certs, cert_reqs = _build_ssl_context(cert_path_obj, verify_ssl)

    # ------------------------------------------------------------------ #
    # 4. Create HTTP pool manager
    # ------------------------------------------------------------------ #
    http = _configure_http_pool(timeout, retry_strategy, ssl_context, ca_certs, cert_reqs)

    # ------------------------------------------------------------------ #
    # 5. Perform the request and follow redirects manually
    # ------------------------------------------------------------------ #
    try:
        response, final_url, n_redirects = _handle_redirects(http, URL(url).href, max_redirects,
                                                             allow_private_ip, validate_host)
    except (ConnectionError, RuntimeError, ValueError) as exc:
        message = f"Request to {url!r} failed: {exc}"
        logger.error(message)
        return ServerCheckResult(
            url=url,
            status_code=0,
            ok=False,
            final_url=url,
            message=message,
        )

    # ------------------------------------------------------------------ #
    # 6. Build the result object
    # ------------------------------------------------------------------ #
    ok = 200 <= response.status < 400
    return ServerCheckResult(
        url=url,
        status_code=response.status,
        ok=ok,
        final_url=final_url,
        message="Valid URL." if ok else f"HTTP {response.status}",
        n_redirects=n_redirects,
    )