url
plantdb.client.url Link
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
|
str
|
The URL of the server to check. |
required |
timeout
|
float
|
The maximum number of seconds to wait for a response. Default is 5.0. |
5.0
|
max_redirects
|
int
|
The maximum number of redirects to follow. Default is 2. |
2
|
max_retries
|
int
|
The maximum number of retries in case of failure. Default is 3. |
3
|
backoff_factor
|
float
|
A factor to multiply the delay between retry attempts. Default is 0.3. |
0.3
|
allow_private_ip
|
bool
|
If True, the checker will accept URLs whose resolved IPs are private or non‑routable. |
False
|
validate_host
|
bool
|
Whether to validate the hostname against a whitelist & blacklist. Default is True. Default is False. |
True
|
cert_path
|
str or Path
|
Path to a CA bundle to use. Default is None. |
None
|
verify_ssl
|
bool
|
Whether to verify SSL certificates. Default is True. |
True
|
Returns:
| Type | Description |
|---|---|
ServerCheckResult
|
An object containing details about the server check result.
|
Examples:
>>> from plantdb.server.test_rest_api import TestRestApiServer
>>> from plantdb.client.url import is_server_available
>>> # EXAMPLE 1: Test with a valid online server
>>> result = is_server_available("https://example.com")
>>> print(result.ok)
True
>>> print(result.url)
https://example.com
>>> # EXAMPLE 2: Test with a valid local IP (allowed for test purposes) and a running server
>>> server = TestRestApiServer(test=True, ssl=False) # initialize the server
>>> server.start() # start the server
>>> result = is_server_available("http://127.0.0.1:5000", allow_private_ip=True)
>>> print(result.ok)
True
>>> print(result.message)
Valid URL.
>>> server.stop() # stop the server
Notes
This function uses the requests library to perform HTTP requests. It handles
retries and redirects according to the parameters provided.
See Also
requests.Session : The class used for sending HTTP requests.
plantdb.client.url._parse_url: the url parsing method.
plantdb.client.url._validate_hostname: the url validation method used if validate_host is True.
Source code in plantdb/client/url.py
552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 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 769 770 771 772 773 | |