nanopyx.core.utils.url_checker

 1import requests
 2
 3
 4def url_checker(url):
 5    """
 6    Check the reachability of a URL by sending an HTTP GET request.
 7
 8    Args:
 9        url (str): The URL to be checked.
10
11    Returns:
12        str: A message indicating whether the URL is reachable or not.
13
14    Raises:
15        SystemExit: If an exception occurs during the HTTP request, the function exits with an error message.
16
17    Example:
18        result = url_checker("https://www.example.com")
19        # Output: "https://www.example.com: is reachable"
20
21    Note:
22        This function sends an HTTP GET request to the specified URL and checks the HTTP status code.
23        If the status code is 200, it indicates that the URL is reachable.
24        If there are any exceptions during the request, it raises a SystemExit with an error message.
25    """
26    try:
27        # Get Url
28        get = requests.get(url)
29        # if the request succeeds
30        if get.status_code == 200:
31            return f"{url}: is reachable"
32        else:
33            return f"{url}: is Not reachable, status_code: {get.status_code}"
34
35    # Exception
36    except requests.exceptions.RequestException as e:
37        # print URL with Errs
38        raise SystemExit(f"{url}: is Not reachable \nErr: {e}")
def url_checker(url):
 5def url_checker(url):
 6    """
 7    Check the reachability of a URL by sending an HTTP GET request.
 8
 9    Args:
10        url (str): The URL to be checked.
11
12    Returns:
13        str: A message indicating whether the URL is reachable or not.
14
15    Raises:
16        SystemExit: If an exception occurs during the HTTP request, the function exits with an error message.
17
18    Example:
19        result = url_checker("https://www.example.com")
20        # Output: "https://www.example.com: is reachable"
21
22    Note:
23        This function sends an HTTP GET request to the specified URL and checks the HTTP status code.
24        If the status code is 200, it indicates that the URL is reachable.
25        If there are any exceptions during the request, it raises a SystemExit with an error message.
26    """
27    try:
28        # Get Url
29        get = requests.get(url)
30        # if the request succeeds
31        if get.status_code == 200:
32            return f"{url}: is reachable"
33        else:
34            return f"{url}: is Not reachable, status_code: {get.status_code}"
35
36    # Exception
37    except requests.exceptions.RequestException as e:
38        # print URL with Errs
39        raise SystemExit(f"{url}: is Not reachable \nErr: {e}")

Check the reachability of a URL by sending an HTTP GET request.

Args: url (str): The URL to be checked.

Returns: str: A message indicating whether the URL is reachable or not.

Raises: SystemExit: If an exception occurs during the HTTP request, the function exits with an error message.

Example: result = url_checker("https://www.example.com") # Output: "https://www.example.com: is reachable"

Note: This function sends an HTTP GET request to the specified URL and checks the HTTP status code. If the status code is 200, it indicates that the URL is reachable. If there are any exceptions during the request, it raises a SystemExit with an error message.