simple2-http-checking 1.0.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- simple2_http_checking-1.0.0/LICENSE +21 -0
- simple2_http_checking-1.0.0/PKG-INFO +36 -0
- simple2_http_checking-1.0.0/README.md +18 -0
- simple2_http_checking-1.0.0/pyproject.toml +76 -0
- simple2_http_checking-1.0.0/setup.cfg +4 -0
- simple2_http_checking-1.0.0/src/simple2_http_checking.egg-info/PKG-INFO +36 -0
- simple2_http_checking-1.0.0/src/simple2_http_checking.egg-info/SOURCES.txt +13 -0
- simple2_http_checking-1.0.0/src/simple2_http_checking.egg-info/dependency_links.txt +1 -0
- simple2_http_checking-1.0.0/src/simple2_http_checking.egg-info/entry_points.txt +2 -0
- simple2_http_checking-1.0.0/src/simple2_http_checking.egg-info/requires.txt +11 -0
- simple2_http_checking-1.0.0/src/simple2_http_checking.egg-info/top_level.txt +1 -0
- simple2_http_checking-1.0.0/src/simple_http_checker/__init__.py +0 -0
- simple2_http_checking-1.0.0/src/simple_http_checker/checker.py +54 -0
- simple2_http_checking-1.0.0/src/simple_http_checker/cli.py +63 -0
- simple2_http_checking-1.0.0/tests/test_checker.py +115 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 ABDELAZIZ MTIMET
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: simple2-http-checking
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: A simple CLI tool to check the status of URLs.
|
|
5
|
+
Description-Content-Type: text/markdown
|
|
6
|
+
License-File: LICENSE
|
|
7
|
+
Requires-Dist: requests<3.0,>=2.28
|
|
8
|
+
Requires-Dist: click<9.0,>=8.0
|
|
9
|
+
Provides-Extra: dev
|
|
10
|
+
Requires-Dist: pytest; extra == "dev"
|
|
11
|
+
Requires-Dist: pytest-mock; extra == "dev"
|
|
12
|
+
Requires-Dist: ruff; extra == "dev"
|
|
13
|
+
Requires-Dist: black; extra == "dev"
|
|
14
|
+
Requires-Dist: mypy; extra == "dev"
|
|
15
|
+
Requires-Dist: bandit; extra == "dev"
|
|
16
|
+
Requires-Dist: types-requests; extra == "dev"
|
|
17
|
+
Dynamic: license-file
|
|
18
|
+
|
|
19
|
+
# Python Projects: CI/CD
|
|
20
|
+
This repo contains the code for the CI/CD sections of my Python for Devops course.
|
|
21
|
+
|
|
22
|
+
## What we implement in this repository
|
|
23
|
+
|
|
24
|
+
[] Implement the project (code files)
|
|
25
|
+
|
|
26
|
+
[] Add a simple GHA workflow and make sure it runs until completion
|
|
27
|
+
|
|
28
|
+
[] Add linting (ruff) and format checks (black)
|
|
29
|
+
|
|
30
|
+
[] Add typing (mypy) and security checks (bandit)
|
|
31
|
+
|
|
32
|
+
[] Add test automation
|
|
33
|
+
|
|
34
|
+
[] Build our Python project
|
|
35
|
+
|
|
36
|
+
[] Publish the project to both TestPyPi and Pypi when a new release is published
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# Python Projects: CI/CD
|
|
2
|
+
This repo contains the code for the CI/CD sections of my Python for Devops course.
|
|
3
|
+
|
|
4
|
+
## What we implement in this repository
|
|
5
|
+
|
|
6
|
+
[] Implement the project (code files)
|
|
7
|
+
|
|
8
|
+
[] Add a simple GHA workflow and make sure it runs until completion
|
|
9
|
+
|
|
10
|
+
[] Add linting (ruff) and format checks (black)
|
|
11
|
+
|
|
12
|
+
[] Add typing (mypy) and security checks (bandit)
|
|
13
|
+
|
|
14
|
+
[] Add test automation
|
|
15
|
+
|
|
16
|
+
[] Build our Python project
|
|
17
|
+
|
|
18
|
+
[] Publish the project to both TestPyPi and Pypi when a new release is published
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=77.0"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "simple2-http-checking"
|
|
7
|
+
version = "1.0.0"
|
|
8
|
+
description = "A simple CLI tool to check the status of URLs."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
dependencies = [
|
|
11
|
+
"requests>=2.28,<3.0",
|
|
12
|
+
"click>=8.0,<9.0"
|
|
13
|
+
]
|
|
14
|
+
|
|
15
|
+
[project.optional-dependencies]
|
|
16
|
+
dev = [
|
|
17
|
+
"pytest",
|
|
18
|
+
"pytest-mock",
|
|
19
|
+
"ruff",
|
|
20
|
+
"black",
|
|
21
|
+
"mypy",
|
|
22
|
+
"bandit",
|
|
23
|
+
"types-requests"
|
|
24
|
+
]
|
|
25
|
+
|
|
26
|
+
[project.scripts]
|
|
27
|
+
check-urls = "simple_http_checker.cli:main"
|
|
28
|
+
|
|
29
|
+
[tool.ruff]
|
|
30
|
+
line-length = 65
|
|
31
|
+
[tool.black]
|
|
32
|
+
line-length = 65
|
|
33
|
+
[tool.mypy]
|
|
34
|
+
warn_return_any = true
|
|
35
|
+
warn_unused_configs = true
|
|
36
|
+
ignore_missing_imports = true
|
|
37
|
+
|
|
38
|
+
[tool.bandit]
|
|
39
|
+
exclude_dirs = [".venv", "__pycache__", "build", "dist"]
|
|
40
|
+
skips = ["B101"]
|
|
41
|
+
|
|
42
|
+
[tool.pytest.ini_options]
|
|
43
|
+
addopts = "-rA -s -v"
|
|
44
|
+
|
|
45
|
+
[tool.semantic_release]
|
|
46
|
+
version_toml = ["pyproject.toml:project.version"]
|
|
47
|
+
build_command = "pip install build && python -m build"
|
|
48
|
+
dist_path = "dist/"
|
|
49
|
+
upload_to_pypi = false
|
|
50
|
+
upload_to_release = true
|
|
51
|
+
hvcs = "github"
|
|
52
|
+
commit_message = "chore(release) : {version} [skip ci]"
|
|
53
|
+
|
|
54
|
+
[tool.semantic_release.branches.main]
|
|
55
|
+
match = "main"
|
|
56
|
+
prerelease = false
|
|
57
|
+
|
|
58
|
+
[tool.semantic_release.changelog.default_templates]
|
|
59
|
+
changelog_file = "CHANGELOG.md"
|
|
60
|
+
|
|
61
|
+
[tool.semantic_release.commit_parser_options]
|
|
62
|
+
allowed_tags = [
|
|
63
|
+
"build",
|
|
64
|
+
"chore",
|
|
65
|
+
"ci",
|
|
66
|
+
"docs",
|
|
67
|
+
"feat",
|
|
68
|
+
"fix",
|
|
69
|
+
"pref",
|
|
70
|
+
"style",
|
|
71
|
+
"refactor",
|
|
72
|
+
"test"
|
|
73
|
+
]
|
|
74
|
+
|
|
75
|
+
minor_tags = ["feat"]
|
|
76
|
+
patch_tags = ["fix", "perf"]
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: simple2-http-checking
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: A simple CLI tool to check the status of URLs.
|
|
5
|
+
Description-Content-Type: text/markdown
|
|
6
|
+
License-File: LICENSE
|
|
7
|
+
Requires-Dist: requests<3.0,>=2.28
|
|
8
|
+
Requires-Dist: click<9.0,>=8.0
|
|
9
|
+
Provides-Extra: dev
|
|
10
|
+
Requires-Dist: pytest; extra == "dev"
|
|
11
|
+
Requires-Dist: pytest-mock; extra == "dev"
|
|
12
|
+
Requires-Dist: ruff; extra == "dev"
|
|
13
|
+
Requires-Dist: black; extra == "dev"
|
|
14
|
+
Requires-Dist: mypy; extra == "dev"
|
|
15
|
+
Requires-Dist: bandit; extra == "dev"
|
|
16
|
+
Requires-Dist: types-requests; extra == "dev"
|
|
17
|
+
Dynamic: license-file
|
|
18
|
+
|
|
19
|
+
# Python Projects: CI/CD
|
|
20
|
+
This repo contains the code for the CI/CD sections of my Python for Devops course.
|
|
21
|
+
|
|
22
|
+
## What we implement in this repository
|
|
23
|
+
|
|
24
|
+
[] Implement the project (code files)
|
|
25
|
+
|
|
26
|
+
[] Add a simple GHA workflow and make sure it runs until completion
|
|
27
|
+
|
|
28
|
+
[] Add linting (ruff) and format checks (black)
|
|
29
|
+
|
|
30
|
+
[] Add typing (mypy) and security checks (bandit)
|
|
31
|
+
|
|
32
|
+
[] Add test automation
|
|
33
|
+
|
|
34
|
+
[] Build our Python project
|
|
35
|
+
|
|
36
|
+
[] Publish the project to both TestPyPi and Pypi when a new release is published
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
README.md
|
|
3
|
+
pyproject.toml
|
|
4
|
+
src/simple2_http_checking.egg-info/PKG-INFO
|
|
5
|
+
src/simple2_http_checking.egg-info/SOURCES.txt
|
|
6
|
+
src/simple2_http_checking.egg-info/dependency_links.txt
|
|
7
|
+
src/simple2_http_checking.egg-info/entry_points.txt
|
|
8
|
+
src/simple2_http_checking.egg-info/requires.txt
|
|
9
|
+
src/simple2_http_checking.egg-info/top_level.txt
|
|
10
|
+
src/simple_http_checker/__init__.py
|
|
11
|
+
src/simple_http_checker/checker.py
|
|
12
|
+
src/simple_http_checker/cli.py
|
|
13
|
+
tests/test_checker.py
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
simple_http_checker
|
|
File without changes
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import logging
|
|
2
|
+
|
|
3
|
+
import requests
|
|
4
|
+
|
|
5
|
+
logger = logging.getLogger(__name__)
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def check_urls(
|
|
9
|
+
urls: list[str], timeout: int = 5
|
|
10
|
+
) -> dict[str, str]:
|
|
11
|
+
"""
|
|
12
|
+
Check the status of a list of URLs.
|
|
13
|
+
|
|
14
|
+
Args:
|
|
15
|
+
urls: A list of URLs to check.
|
|
16
|
+
timeout: The timeout for each HTTP request, in seconds.
|
|
17
|
+
|
|
18
|
+
Returns:
|
|
19
|
+
A dictionary mapping each URL to its status
|
|
20
|
+
(an HTTP status code, or a short error description).
|
|
21
|
+
"""
|
|
22
|
+
logger.info(
|
|
23
|
+
f"Checking {len(urls)} URLs with a timeout of {timeout} seconds."
|
|
24
|
+
)
|
|
25
|
+
results: dict[str, str] = {}
|
|
26
|
+
|
|
27
|
+
for url in urls:
|
|
28
|
+
try:
|
|
29
|
+
logger.debug(f"Checking URL: {url}")
|
|
30
|
+
response = requests.get(url, timeout=timeout)
|
|
31
|
+
|
|
32
|
+
if response.ok:
|
|
33
|
+
status = str(response.status_code)
|
|
34
|
+
else:
|
|
35
|
+
status = (
|
|
36
|
+
f"{response.status_code} - {response.reason}"
|
|
37
|
+
)
|
|
38
|
+
except requests.exceptions.Timeout:
|
|
39
|
+
status = "TIMEOUT"
|
|
40
|
+
logger.warning(f"Timeout occurred for URL: {url}")
|
|
41
|
+
except requests.exceptions.ConnectionError:
|
|
42
|
+
status = "CONNECTION ERROR"
|
|
43
|
+
logger.warning(
|
|
44
|
+
f"Connection error occurred for URL: {url}"
|
|
45
|
+
)
|
|
46
|
+
except requests.exceptions.RequestException as e:
|
|
47
|
+
status = f"ERROR - {type(e).__name__}"
|
|
48
|
+
logger.exception(f"Request exception for URL: {url}")
|
|
49
|
+
|
|
50
|
+
results[url] = status
|
|
51
|
+
logger.debug(f"Checked: {url:<40} Status: {status}")
|
|
52
|
+
|
|
53
|
+
logger.info("URL checking completed.")
|
|
54
|
+
return results
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import logging
|
|
2
|
+
import sys
|
|
3
|
+
from _collections_abc import Collection
|
|
4
|
+
|
|
5
|
+
import click
|
|
6
|
+
|
|
7
|
+
from simple_http_checker.checker import check_urls
|
|
8
|
+
|
|
9
|
+
logger = logging.getLogger(__name__)
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
@click.command(
|
|
13
|
+
name="check_urls",
|
|
14
|
+
help="Check the status of one or more URLs.",
|
|
15
|
+
)
|
|
16
|
+
@click.argument("urls", nargs=-1, required=True)
|
|
17
|
+
@click.option(
|
|
18
|
+
"--timeout",
|
|
19
|
+
type=click.IntRange(min=1),
|
|
20
|
+
default=5,
|
|
21
|
+
show_default=True,
|
|
22
|
+
help="Timeout for each request, in seconds.",
|
|
23
|
+
)
|
|
24
|
+
@click.option(
|
|
25
|
+
"--verbose",
|
|
26
|
+
"-v",
|
|
27
|
+
is_flag=True,
|
|
28
|
+
help="Show detailed debug logs.",
|
|
29
|
+
)
|
|
30
|
+
def main(urls: Collection[str], timeout: int, verbose: bool):
|
|
31
|
+
"""Check the status of one or more URLs."""
|
|
32
|
+
logging.basicConfig(
|
|
33
|
+
level=logging.DEBUG if verbose else logging.WARNING,
|
|
34
|
+
format="%(levelname)s:%(name)s:%(message)s",
|
|
35
|
+
force=True,
|
|
36
|
+
)
|
|
37
|
+
if verbose:
|
|
38
|
+
logging.getLogger().setLevel(logging.DEBUG)
|
|
39
|
+
logger.debug("Verbose mode enabled.")
|
|
40
|
+
logger.debug(f"Received urls: {urls}")
|
|
41
|
+
logger.debug(f"Received timeout: {timeout} seconds")
|
|
42
|
+
logger.debug(f"Received verbose flag: {verbose}")
|
|
43
|
+
if not urls:
|
|
44
|
+
logger.error("No URLs provided.")
|
|
45
|
+
click.echo("Error: No URLs provided.", err=True)
|
|
46
|
+
sys.exit(1)
|
|
47
|
+
results = check_urls(list(urls), timeout=timeout)
|
|
48
|
+
|
|
49
|
+
failed = 0
|
|
50
|
+
for url, status in results.items():
|
|
51
|
+
ok = status.isdigit()
|
|
52
|
+
if not ok:
|
|
53
|
+
failed += 1
|
|
54
|
+
click.secho(
|
|
55
|
+
f"{url:<40} {status}", fg="green" if ok else "red"
|
|
56
|
+
)
|
|
57
|
+
|
|
58
|
+
click.echo(
|
|
59
|
+
f"\n{len(results) - failed}/{len(results)} URLs OK"
|
|
60
|
+
)
|
|
61
|
+
|
|
62
|
+
if failed:
|
|
63
|
+
sys.exit(1)
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import pytest
|
|
2
|
+
import requests
|
|
3
|
+
from pytest_mock import MockerFixture
|
|
4
|
+
|
|
5
|
+
from simple_http_checker.checker import check_urls
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def test_check_urls_success(mocker: MockerFixture):
|
|
9
|
+
# Mock the requests.get method to simulate a successful response
|
|
10
|
+
mock_response = mocker.MagicMock(spec=requests.Response)
|
|
11
|
+
mock_response.ok = True
|
|
12
|
+
mock_response.status_code = 200
|
|
13
|
+
mock_response.reason = "OK"
|
|
14
|
+
mocker.patch(
|
|
15
|
+
"simple_http_checker.checker.requests.get",
|
|
16
|
+
return_value=mock_response,
|
|
17
|
+
)
|
|
18
|
+
|
|
19
|
+
urls = ["http://example.com"]
|
|
20
|
+
results = check_urls(urls)
|
|
21
|
+
|
|
22
|
+
assert results == {"http://example.com": "200"}
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def test_check_urls_client_error(mocker: MockerFixture):
|
|
26
|
+
# Mock the requests.get method to simulate a client error response
|
|
27
|
+
mock_response = mocker.MagicMock(spec=requests.Response)
|
|
28
|
+
mock_response.ok = False
|
|
29
|
+
mock_response.status_code = 404
|
|
30
|
+
mock_response.reason = "Not Found"
|
|
31
|
+
mocker.patch(
|
|
32
|
+
"simple_http_checker.checker.requests.get",
|
|
33
|
+
return_value=mock_response,
|
|
34
|
+
)
|
|
35
|
+
|
|
36
|
+
urls = ["http://example.com/nonexistent"]
|
|
37
|
+
results = check_urls(urls)
|
|
38
|
+
|
|
39
|
+
assert results == {
|
|
40
|
+
"http://example.com/nonexistent": "404 - Not Found"
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
@pytest.mark.parametrize(
|
|
45
|
+
"error_exception, expected_status",
|
|
46
|
+
[
|
|
47
|
+
(requests.exceptions.Timeout, "TIMEOUT"),
|
|
48
|
+
(
|
|
49
|
+
requests.exceptions.ConnectionError,
|
|
50
|
+
"CONNECTION ERROR",
|
|
51
|
+
),
|
|
52
|
+
(
|
|
53
|
+
requests.exceptions.RequestException,
|
|
54
|
+
"ERROR - RequestException",
|
|
55
|
+
),
|
|
56
|
+
],
|
|
57
|
+
)
|
|
58
|
+
def test_check_urls_exception(
|
|
59
|
+
mocker: MockerFixture,
|
|
60
|
+
error_exception: Exception,
|
|
61
|
+
expected_status: str,
|
|
62
|
+
):
|
|
63
|
+
# Mock the requests.get method to raise a RequestException
|
|
64
|
+
mocker.patch(
|
|
65
|
+
"simple_http_checker.checker.requests.get",
|
|
66
|
+
side_effect=requests.exceptions.RequestException,
|
|
67
|
+
)
|
|
68
|
+
|
|
69
|
+
urls = ["http://MYDOG.com"]
|
|
70
|
+
results = check_urls(urls)
|
|
71
|
+
|
|
72
|
+
assert results == {
|
|
73
|
+
"http://MYDOG.com": "ERROR - RequestException"
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def test_check_urls_with_multiple_urls(mocker: MockerFixture):
|
|
78
|
+
# Mock the requests.get method to simulate successful responses
|
|
79
|
+
mock_response = mocker.MagicMock(spec=requests.Response)
|
|
80
|
+
mock_response.ok = True
|
|
81
|
+
mock_response.status_code = 200
|
|
82
|
+
mock_response.reason = "OK"
|
|
83
|
+
mocker.patch(
|
|
84
|
+
"simple_http_checker.checker.requests.get",
|
|
85
|
+
return_value=mock_response,
|
|
86
|
+
)
|
|
87
|
+
|
|
88
|
+
urls = ["http://example.com", "http://mydog.com"]
|
|
89
|
+
results = check_urls(urls)
|
|
90
|
+
|
|
91
|
+
assert results == {
|
|
92
|
+
"http://example.com": "200",
|
|
93
|
+
"http://mydog.com": "200",
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
def test_check_urls_empty_list(mocker: MockerFixture):
|
|
98
|
+
# Test with an empty list of URLs
|
|
99
|
+
urls = []
|
|
100
|
+
results = check_urls(urls)
|
|
101
|
+
|
|
102
|
+
assert results == {}
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
def test_check_urls_timeout(mocker: MockerFixture):
|
|
106
|
+
# Mock the requests.get method to raise a Timeout exception
|
|
107
|
+
mocker.patch(
|
|
108
|
+
"simple_http_checker.checker.requests.get",
|
|
109
|
+
side_effect=requests.exceptions.Timeout,
|
|
110
|
+
)
|
|
111
|
+
|
|
112
|
+
urls = ["http://example.com"]
|
|
113
|
+
results = check_urls(urls)
|
|
114
|
+
|
|
115
|
+
assert results == {"http://example.com": "TIMEOUT"}
|