-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathurl_test
More file actions
31 lines (26 loc) · 1.12 KB
/
Copy pathurl_test
File metadata and controls
31 lines (26 loc) · 1.12 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
#!/usr/bin/env python3
# Checks if the url responds with 200. Used to detect dead links and/or faulty urls.
# In a terminal, run with 'python3 urltest.py urls.txt' where urls.txt is a textfile with urls.
import re
import sys
from urllib.request import Request, urlopen
from urllib.error import URLError
all_urls_valid = True # Initialize flag to track if all URLs are valid
with open(sys.argv[1], 'r') as f:
for line in f.readlines():
if 'http' in line:
for url in re.findall("(http[s?]://[^)]+)", line):
try:
request = Request(url)
request.get_method = lambda: 'HEAD'
resp = urlopen(request)
status = resp.getcode()
print(f"{url} returned status {status}")
if status != 200:
all_urls_valid = False
print(f"{url} returned status {status}")
except URLError as e:
print(f"{url} returned error '{e}'")
all_urls_valid = False
if all_urls_valid:
print("All URLs are valid.")