-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathurl-checker.py
More file actions
146 lines (125 loc) · 4.82 KB
/
url-checker.py
File metadata and controls
146 lines (125 loc) · 4.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/usr/bin/env python3
"""
URL Checker — Check if URLs are alive by HTTP status.
Usage:
python url-checker.py <url> [<url> ...]
python url-checker.py --file urls.txt
python url-checker.py --file urls.txt --timeout 10 --concurrent 20
Options:
--file FILE Read URLs from file (one per line)
--timeout SEC Request timeout in seconds (default: 10)
--concurrent N Number of concurrent checks (default: 10)
--follow-redirects Follow redirects (default: True)
--help Show this help message and exit
"""
import sys
import argparse
from pathlib import Path
from concurrent.futures import ThreadPoolExecutor, as_completed
try:
import requests
except ImportError:
print("Error: requests is required. Install with: pip install requests")
sys.exit(1)
def check_url(url: str, timeout: int, follow: bool) -> dict:
"""Check a single URL and return status info."""
result = {"url": url, "status": None, "error": None, "redirect": None}
try:
resp = requests.get(
url,
timeout=timeout,
allow_redirects=follow,
headers={"User-Agent": "URL-Checker/1.0"},
)
result["status"] = resp.status_code
if resp.history:
result["redirect"] = resp.url
except requests.exceptions.ConnectionError:
result["error"] = "Connection refused / unreachable"
except requests.exceptions.Timeout:
result["error"] = f"Timed out ({timeout}s)"
except requests.exceptions.TooManyRedirects:
result["error"] = "Too many redirects"
except requests.exceptions.MissingSchema:
result["error"] = "Invalid URL (missing scheme like http://)"
except Exception as e:
result["error"] = str(e)
return result
def status_label(status: int | None) -> str:
"""Return a colored label for an HTTP status code."""
if status is None:
return "ERROR"
if 200 <= status < 300:
return "ALIVE"
elif 300 <= status < 400:
return "REDIR"
elif 400 <= status < 500:
return "CLIENT_ERROR"
elif 500 <= status < 600:
return "SERVER_ERROR"
return f"UNKNOWN({status})"
def main():
parser = argparse.ArgumentParser(
description="Check if URLs are alive by HTTP status.",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog=(
"Examples:\n"
" python url-checker.py https://google.com https://github.com\n"
" python url-checker.py --file my_urls.txt\n"
" python url-checker.py --file urls.txt --concurrent 20 --timeout 5\n"
),
)
parser.add_argument("urls", nargs="*", help="URL(s) to check")
parser.add_argument("--file", help="File containing URLs (one per line)")
parser.add_argument("--timeout", type=int, default=10, help="Request timeout in seconds (default: 10)")
parser.add_argument("--concurrent", type=int, default=10, help="Concurrent checks (default: 10)")
parser.add_argument("--follow-redirects", action="store_true", default=True, help="Follow redirects")
args = parser.parse_args()
url_list: list[str] = list(args.urls)
if args.file:
file_path = Path(args.file)
if not file_path.is_file():
print(f"Error: File '{args.file}' not found.")
sys.exit(1)
with open(file_path, "r") as f:
for line in f:
line = line.strip()
if line and not line.startswith("#"):
url_list.append(line)
if not url_list:
print("Error: No URLs provided. Pass them as arguments or with --file.")
parser.print_help()
sys.exit(1)
print(f"Checking {len(url_list)} URL(s) (timeout={args.timeout}s, concurrent={args.concurrent})\n")
results: list[dict] = []
with ThreadPoolExecutor(max_workers=args.concurrent) as executor:
futures = {
executor.submit(check_url, url, args.timeout, args.follow_redirects): url
for url in url_list
}
for future in as_completed(futures):
results.append(future.result())
# Summary
alive = dead = redirects = errors = 0
for r in results:
label = status_label(r["status"])
if label == "ALIVE":
alive += 1
elif label == "REDIR":
redirects += 1
elif "error" in r and r["error"]:
errors += 1
else:
dead += 1
msg = f" {r['url']:50s} {label}"
if r["status"]:
msg += f" [{r['status']}]"
if r.get("redirect"):
msg += f" → {r['redirect']}"
if r.get("error"):
msg += f" ✗ {r['error']}"
print(msg)
print(f"\nSummary: {alive} alive, {redirects} redirects, {dead} dead, {errors} errors "
f"(out of {len(results)})")
if __name__ == "__main__":
main()