-
Notifications
You must be signed in to change notification settings - Fork 0
/
check301.py
43 lines (36 loc) · 1.19 KB
/
check301.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
# requires requests lib
# - pip install requests
import requests
import sys
def redirectTest(url):
with open("no_redirects.txt", "a") as no_redirect:
try:
r = requests.head(url, allow_redirects=False)
if (r.status_code == 301):
print("+ %s :: %d" % (url, r.status_code))
else:
print("- WARNING: %s :: %d" % (url, r.status_code))
no_redirect.write("%s :: %d\n" % (url, r.status_code))
except requests.exceptions.RequestException as e:
print("! Error with request: %s :: %s" % (url, e))
def load_urls(urlfile):
# clean urls of white space and remove empty lines
# this does not prepend http/https to urls missing them
clean_urls = []
with open(urlfile) as f:
urllist = f.readlines()
for i in urllist:
i = i.strip()
if i:
clean_urls.append(i)
return (clean_urls)
args = sys.argv
if len(args) < 2:
print("! Error: Missing arguments.\n\nUsage: python check301.py urls.txt")
else:
try:
l = load_urls(args[1])
for i in l:
redirectTest(i)
except Exception as e:
print("Something went wrong: %s" % e)