forked from muellermartin/nosystemd.org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
checklinks.py
executable file
·65 lines (50 loc) · 1.73 KB
/
checklinks.py
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
#!/usr/bin/env python3
import sys
from bs4 import BeautifulSoup
from colorama import Fore, Style
import colorama
import requests
def checklink(url, headers=None):
try:
response = requests.head(url, allow_redirects=True, headers=headers)
if response.status_code == 200:
ok = True
# 403 = forbidden
# Some shitty WAFs seem to dislike Python as user agent
# -> fake user agent
elif response.status_code == 403:
user_agent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36'
return checklink(url, headers={'User-Agent': user_agent})
# 405 = method not allowed
# -> retry with GET
elif response.status_code == 405:
response = requests.get(url, headers=headers)
if response.status_code == 200:
ok = True
else:
ok = False
else:
ok = False
except requests.exceptions.SSLError:
ok = False
print('SSL error for {}'.format(url), file=sys.stderr)
except requests.ConnectionError:
ok = False
print('Can\'t connect to {}'.format(url), file=sys.stderr)
if ok:
print(Fore.GREEN + 'OK' + Style.RESET_ALL)
return True
print(Fore.RED + 'FAIL' + Style.RESET_ALL)
return False
def main():
with open('htdocs/index.html') as f:
html_doc = f.read()
soup = BeautifulSoup(html_doc, 'html.parser')
for anchor in soup.find_all('a'):
url = anchor['href']
if url.startswith('http'):
print('Checking URL {}'.format(url))
checklink(url)
if __name__ == '__main__':
colorama.init()
main()